#Help migrating Node & Express API from heroku to Railway

11 messages · Page 1 of 1 (latest)

lucid stump
#

Hi! I'm a frontend developer learning more about backend work. I followed a tutorial to create a MERN-stack app, and part of that included deploying my node/express API to Heroku using the Heroku CLI. Now that Heroku is removing their free-tier plans, I'm trying out Railway to host the API instead.

I followed the instructions from the Railway docs to set up my project using my Github monorepo. Here are some of my custom settings:
Root directory: /api
Start command: node index.js

I think where I'm stuck is the build command. I didn't need a build command for heroku, so I guess I'm wondering what Railway needs out of the build? When I build React projects, the build command generally outputs plain HTML, CSS, and JS files that the browser can serve—so what transformations are needed for an API build?

I don't even know if I'm asking the right question... so please correct me or send me reading materials or examples if you have them!

When I try to deploy, I see "connected to mongoose" in the logs, which, when I start my app locally, is how I know that it's started properly. But I think Railway is actually expecting a build process to occur once and finish, so it eventually times out and says that the build failed. When I'm in the middle of "deploying" and send a postman request to the URL, it shows the request in the logs but Postman never receives the response.

I guess I am just pretty stuck and I don't know what to try or even what to look up.

Project ID: b566b694-cdf9-49b1-b8f1-cf33649dffef

last islandBOT
#

Project ID: b566b694-cdf9-49b1-b8f1-cf33649dffef

high tundra
#

Hey there @lucid stump, sorry for the wait. Can you publish your package.json?

#

It seems that your build script has a

#13 1.048 > [email protected] build
#13 1.048 > echo "Error: no build command specified" && exit 1

Exit 1 in the command which will make it fail no matter what.

lucid stump
#

Hey, no worries!
Yeah, right now it doesn't have a build command:

{
  "name": "api",
  "version": "1.0.0",
  "description": "the backend for the tattoo app",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "echo \"Error: no build command specified\" && exit 1",
    "start": "node index.js",
    "watch": "nodemon index.js"
  },
  "author": "Christina",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.26.1",
    "bcrypt": "^5.0.1",
    "body-parser": "^1.19.2",
    "cors": "^2.8.5",
    "dotenv": "^16.0.0",
    "express": "^4.17.3",
    "helmet": "^5.0.2",
    "mongoose": "^6.2.7",
    "morgan": "^1.10.0"
  }
}
#

I guess I'm wondering what the build command should be/do?
When I deployed to Heroku I didn't need to build it that I know of?
My Heroku Procfile is just one line: web: node index.js

dusk juniper
#

if your app doesn't need to build then that is okay! Nixpacks runs the build script in package.json by default, which is failing in your app (because of the exit 1). You can simply remove the build script and it should build and deploy no problem

lucid stump
#

Oh you're right, that worked! Thanks so much!
It's still not working totally properly, though. When I send a GET request in postman with parameters, my heroku one fetches data, whereas my Railway one does not. It loads for quite a while (maybe forever?). The only difference in the postman request is the railway URL vs. the heroku URL.

https://tattoo-production.up.railway.app/api/v1/tattooShops/getWithinRadius?lng=-122.330062&lat=47.603832&distance=12&units=mi

When I run this in postman I get this Log output, and it loads forever:
params { lng: '-122.330062', lat: '47.603832', distance: '12', units: 'mi' }
(I think I console.log params in my api code)

When I query the same thing, but to the heroku URL, I get a response almost immediately.
https://tattooappapi.herokuapp.com/api/v1/tattooShops/getWithinRadius?lng=-122.330062&lat=47.603832&distance=12&units=mi

dusk juniper
#

Hmm that is interesting. I think the issue may be that the mongo url is incorrect as the logs show that mongo is timing out

lucid stump
#

Ah, I copy/pasted my mongo url from my .env file, and it had quotation marks around it. It looks like if I remove them and save the variable it deploys correctly. Now I'm getting errors but I'm pretty sure it's due to a recent change in my query to mongodb.

#

Thanks so much for your help, both of you! I really appreciate it!