This is how can i run my server nodejs , i need to liverealord my server when i make changes to the code in the front-end dev
"start": "node server.js"
This is how can i run my server nodejs , i need to liverealord my server when i make changes to the code in the front-end dev
"start": "node server.js"
Restarting server is one thing, refreshing browser is another thing. For server watching I use nodemon. Nodemon can see when changes occur in any types of files. But nodemon cannot refresh browser page. For this I use browser sync.
I use both in gulp.
So, dependencies from package.json to make it work:
In server file (my server is in ./bin/www, yours can be in server.js, app.js or elsewhere), express server listens to port 3001.
Next thing is to run nodemon and browser sync in gulp. Full contents of gulpfile.js
When running gulp in terminal, it will start watching server as well as refreshing browser on change in any files.
Although we specify port 3001 in express server, our app will be working on port 3002, as we write in browser-sync. 3001 will be used as proxy.
An example from my setup:
livereload.js (so this would be your server.js, of course only use the parts related to livereload, no need to replace your development server)
It actually starts two servers on localhost: the livereload server listening on
:35729
and a static file server on:3033
.Livereload observes the
dist
directory which contains the compiled files (js, css, html). You need to add this snippet to every HTML page that should reload:If you don't transpile/compile/preprocess your js/css/html code (i.e. you directly edit the files that are served) then observe the source directory and you're done. Otherwise you need a task that watches the source directory for changes and compiles to the dist directory which is observed by livereload :)
Relevant parts of my package.json:
$ npm run watch
builds the project and starts the livereload + static file servers. (thebuild:*
tasks omitted for brevity).Use the npm package called livereload.
Use it in conjunction with nodemon so both client side and server side work flawlessly.
--save-dev. I know, I know!
Add browser extension. Available for Safari, Firefox, and Google Chrome. Get them here.
Make sure to have this scripts inside
package.json
.}
server.js
is my entry point.Inside the
server.js
add the following:server.js
is the file I want livereload to watch. You can add any directory instead of a file as well.In terminal:
npm start
Click on the extension icon in the browser to connect.
You can also use livereload and nodemon separately in different terminals.
npm start
npm livereload
npm livereload -p PORT_NUMBER
if default is port is already used.Update: sometimes it doesn't work on saving once. A couple more of Ctrl+S reloads again and makes the changes. I don't know if this is a browser caching issue or package issue.
first:
next add a script line to your package.json
now when you npm live it'll live reload
for more details see https://github.com/remy/nodemon
update if live page reload is also needed
for more details see https://github.com/napcs/node-livereload
If
grunt
is used, there is a npm packagegrunt-contrib-watch
for live reloading.Check out another one called
grunt-express-server
that can work together.and add your main js
for example
and add your index page before body close tag.
and start your server on termial instead node server.js