diegohaz / rest

REST API generator with Node.js, Express and Mongoose
MIT License
1.79k stars 311 forks source link

cannot run through node . or npm start, trying to deploy on azure windows #165

Closed hackerunet closed 6 years ago

hackerunet commented 6 years ago

Hi there, I've been always running this app in dev mode, and now I'm stuck trying to get it to production, so I have some configs in the .env file to make it run but if I run for example npm run prod it , it runs without issues but if I try to run directly with node . I got this error:

xxxx@MrRobot:~/Documentos/projects/Plataforma$ node .
(node:1124) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
MongoDB connection error: MongoError: failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]

I need to run this app calling app.js directly because it's going to be run using azure and iisnode server, so I'm going to attach my current .env file maybe you can see if there is any missing configs

SENDGRID_KEY=key
MASTER_KEY=ND35EpKEVIOzFK49k4h9hp6BfFdjb4jk
JWT_SECRET=PLK0zoe66h9SZor6t0NILrvF96ovhggQ
NODE_ENV=prod
PORT=8080
IP=0.0.0.0
diegohaz commented 6 years ago

The error is related to MongoDB not being accessible. Are you running mongo on your server and passing the correct address to the production key of config.js?

To use npm start, you need to set the environment variables on your server. I don't know much about Azure, but a quick search led me to this page: https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-how-to-specify-environment-variables

hackerunet commented 6 years ago

I'm not aware if more env variables in config.js, all I have is those posted before and also my database is in local env I can access using the npm run dev without issues. I think the documentation is not enough for people that are not totally familiar with spliting configurations with nodejs, like myself, can you please give me a full list of needed variables and where to place them to be able to change between envs ?? I was developing using npm run dev, now I can run npm run prod but after it, I'm also trying in linux running the script with node . but it throws errors. Is totally necessary to run this restful application using npm only?? If I don't use npm to run the scripts is there any possibility that the application missed all config variables just calling node . ??? Another question I have is, what exactly the npm run prod does? does it minimize any JS file? how can we get this to production and of course optimizing the performance?? Thanks

diegohaz commented 6 years ago

First, the error is related to MongoDB. It has nothing to do with env vars. You need to set it up on your server and put the correct address into config.js.

I was just talking about env vars because the .env file will not work on your server (Azure, AWS, Heroku etc.). You need to set env directly there. You can watch the video where I set it on Heroku.

hackerunet commented 6 years ago

What's the ENV variable name to properly configure mongo db uri and options from config.js file? that's not in the README and of course is obvious for most developers but not as clear for newbies like myself, but, I was trying to add mongo.uri in config.js and didn't work, I'm receiving undefined URI in mongoose service which means cannot load the ENV variable to connect to the database when I switch to prod

diegohaz commented 6 years ago

You need to set MONGODB_URI or change config.js: https://github.com/diegohaz/rest/blob/master/generators/app/templates/src/config.js#L62-L68