Reload Express.js application on file save

When you start with Node.js and Express you are probably friends with
CTRL + S (Save file)

CMD + Tab (Mac, switch windows)

CTRL + C (Stop server/application in terminal)

UP ARROW then ENTER (Restart server/application)

However there are tools to do this for you, one is Nodemon, and you can install this globably with
npm install -g nodemon
For your standard Node.js app which you start with node server.js you change that call to nodemon server.js. Now, each time you make a change Nodemon will handle restarting the app … think of it frantically calling CTRL+C, UP ARROW & ENTER in the background for you.
For Express.js the setup is slightly different. From the terminal you would use something like this (Mac/Linux) npm start or DEBUG=myApp npm start to get your project up and running on http://localhost:3000. To use Nodemon here you need to edit your projects package.json file so it looks like this

  “scripts”:{

    // “start”: “node ./bin/www”

    “start”: “nodemon ./bin/www”

   }
Once that’s done, start your Express.js project the normal way and each time you make a change you can go straight to the browser and refresh the page.

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s