简体   繁体   中英

change Express generated app.js file to server.js

I am using the command express -e myAppName to start up my app.

By default this gives me a file named app.js which as you all know holds all my server logic, middleware, etc.

I tried changing the name of app.js to server.js and after doing so I got this error. Error: Cannot find module '../app'

So I go into my bin directory and inside the www file I change var app = require('../app'); to var app = require('../server'); however that did not fix the error so it persisted.

I am relatively new to web development (3 months) and almost 2 months working with the MEAN stack. I looked around for a while and didn't find anyone attempting to change the file names like I want.

What you did is correct (ie updating the path in bin\\www).

However, you should start your express app using npm start

In general, npm apps allow you to specify a command inside package json which will start your application and this is the first place to look.

For an express generated this should look like the following:

{
  "scripts" : {
    "start" : "node bin/www"
  }
}

If you want to change it and pass arguments later all you have to do is update your package.json file and your end users will not be affected.

npm start gives users of your program a consistent way to start the application regardless of what filenames or parameters you change

setup for development

In your case, since you are using nodemon to automatically restart while you develop, you should use it as follows:

nodemon bin/www

instead of modify your entry point you should make a special script like this:

{
  "scripts" : {
    "start" : "node bin/www",
    "devel" : "nodemon bin/www"
  }
}

Then to start nodemon use:

npm run devel

This way your end user does not start with the nodemon development tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM