最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node JS server not reflecting changes in server file - Stack Overflow

programmeradmin3浏览0评论

Iam new to Node JS when i make any changes to the node file it is not updating in the server and also when i change the port number and kill the server and start it again it is not working

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello');
}).listen(8080); 

Iam new to Node JS when i make any changes to the node file it is not updating in the server and also when i change the port number and kill the server and start it again it is not working

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello');
}).listen(8080); 
Share Improve this question asked May 23, 2020 at 13:41 user11640506user11640506 972 silver badges9 bronze badges 1
  • "when i make any changes to the node file it is not updating in the server" What do you mean by that? If you mean you're expecting it to change immediately, it won't. The code is read into memory and executed from memory; if you change the code file, you have to stop Node.js and start ig again. "when i change the port number and kill the server and start it again it is not working" That will definitely work, I think you must have forgotten to stop it or something like that. – T.J. Crowder Commented May 23, 2020 at 13:44
Add a ment  | 

3 Answers 3

Reset to default 5

I have found a quick solution to fix this thing. You have to install a node module called as nodemon. This module helps to automatically restart your Server once you made the changes in Source code.

npm install -g nodemon

And nodemon will be installed globally to your system path.

You can also install nodemon as a development dependency:

npm install --save-dev nodemon

By default, node does not automatically restart when there are any changes in the file. You might want to try the package nodemon, which can watch for file changes and auto restart the server.

You can try adding an host name to your listen function

let http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

发布评论

评论列表(0)

  1. 暂无评论