I am starting to learn Node.js and one of the annoying things I am encountering is the starting and stopping of the server when I make a small change to the .js file. Any alternatives?
You can try installing
npm install -g nodemon
And then you run your server
nodemon server.js localhost 8080
That automatically makes you restart the server every time you save new changes
More Info. Nodemon
Use something that watches for file changes and automatically restarts node. nodemon is a good choice.
$ sudo npm install -g nodemon
$ nodemon app.js
server.close
Do not call close before the "listening" event fires.
Either add a callback to listen or add an event manually
server.listen(port, host, function () { server.close(); });
// OR
server.on("listening", function () { server.close(); });
server.listen(port, host);
var net = require('net');
var server = net.createServer(function(socket) {
socket.on('data', console.log);
server.close();
});
server.listen(32323);
var socket = net.createConnection(32323);
// call end to make sure the socket closes
socket.end('hi');
There are nice pre-build modules that can do this for you. Check out nodemon, for example -- install it with npm: npm install -g nodemon
Instead of running node app.js you run nodemon app.js; it will automatically restart your application whenever you make a change.
Related
i am new in node.js and i want to create chat system so anybody help me that how can i run my codes on local and on server
Server - (app.js):
var io = require('socket.io')(80);
io.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('disconnect', function () { });
});
Client - (index.html):
<script>
var socket = io('http://localhost/');
socket.on('connect', function () {
socket.send('hi');
socket.on('message', function (msg) {
// my msg
});
});
</script>
what is server (app.js) and how can i run this on my server and how can i call this function to client ?
Step 1. Install node.
Download package from https://nodejs.org/en/, then you got npm and node file.
Create soft link to /usr/local/ for npm and node file.
Step 2. Crate your app.js, and typing:
node app.js
then, your code is running. And you can test in your frontend.
If you wanna your code running background, you can use pm2.
npm install -g pm2
After that, pm2 start app.js. Your code is running background.
See https://www.tutorialspoint.com/socket.io/socket.io_hello_world.htm
You need node installed on your server as a prerequisite.
Once you have nodejs installed, the app can be executed as node app.js
I am trying to make my processes (webpack, nodemon-restart) work with a single gulp command. This works well enough. However, webpack builds only once if its task is tied to gulp's default task (together with nodemon), or embedded withing nodemon's gulp task.
Then I decided to tie both webpack build task and nodemon restart task to gulp's watch command and this works just the way I wanted, except that if you make changes and save them more than twice, the app nodemon crashed and prints this error in the console
"/home/nnanyielugo/Workspace/activity-calendar/node_modules/nodemon/lib/monitor/match.js:132
var rules = monitor.sort(function (a, b) {
^
TypeError: Cannot read property 'sort' of undefined"
As a solution, i tried to tie the webpack build task to the nodemon restart using the .on() method, and instead got an infinite loop of restarting an rebuilding (nodemon restarts first, webpack builds, nodemon restarts again, webpack rebuilds, and on and on).
Does anyone have a solution please?`
Here is a sample of my code `
var gulp = require('gulp'),
nodemon = require('gulp-nodemon'),
webpack = require('webpack-stream');
gulp.task('default', ['watch']);
gulp.task('webpack', function() {
return gulp.src('src/entry.js')
.pipe(webpack(require('./webpack.config.js')))
.pipe(gulp.dest('./public'));
});
gulp.task('nodemon', function () {
return nodemon({
script: 'app.js'
, ext: 'js html'
, env: { 'NODE_ENV': 'development' }
})
})
gulp.task('watch', function(){
gulp.watch(['./api/**/*.js', './server/**/*.js', './*.js'], ['webpack', 'nodemon']);
})`
I guess, your nodemon and gulp's watch task collides with each other. Either you should get ride of using nodemon and to rely upon gulp to start your application.
Or else, you can get rid of your gulp's watch task and add the relevant script in your nodemon's restart method like this,
nodemon({
// script goes here.
}).on('restart', your_reload_logic)
Hope this helps!
I'm confused. So if I use gulp-develop-server, it's got a app.listen():
gulpfile.js
config.server.path is set to './app,js'
server = require('gulp-develop-server');
gulp.task('default', ['server:start'], function(){
});
gulp.task('server:start', function() {
server.listen({ path: config.server.path});
});
My app.js has this:
var koa = require('koa')();
koa.listen(config.server.port, function(){
console.log('Koa app is listening on port ' + config.server.port);
});
so I'm trying to understand better how node is being started. I see people mention doing it manually like "node app.js". So doesn't koa.listen() automatically do a "node" command to start the koa web server? If I use gulp-develop-server and specify server.listen, isn't that doing 2 server.listen() for node?
Just trying to understand the basics here and can't understand why anyone would manually type in 'node [file with .listen]' manually. I'm not doing that manually and server.listen() obviously uses the 'node' command on my app.js.
Your gulpfile is a Node script. So when you run gulp server:start you're executing a Node application, the gulp command is essentially node plus some extra functionality.
The way gulp-develop-server works is it runs an additional Node application as a child process. server.listen is basically just telling gulp-develop-server what script to run.
The naming is a little confusing, but essentially what's going on is: You have 2 Node applications running on your machine (one that you can see, and one in the background), but only 1 server.
I'm new to Node.js and wish to run a program using streams. With other programs, I had to start a server simultaneously (mongodb, redis, etc) but I have no idea if I'm supposed to run one with this. Please let me know where I am going wrong and how I can rectify this.
This is the program:
var http = require('http'),
feed = 'http://isaacs.iriscouch.com/registry/_changes?feed=continuous';
function decide(cb) {
setTimeout(function () {
if (Date.now()%2) { return console.log('rejected'); }
cb();
}, 2000);
}
http.get(feed, function (res) {
decide(res.pipe.bind(res, process.stdout));
//using anonymous function instead of bind:
// decide(function () {
// res.pipe(process.stdout)
// });
});
This is the cmd output:
<b>C:\05-Employing Streams\05-Employing Streams\23-Playing with pipes>node npm_stre
am_piper.js
events.js:72
throw er; // Unhandled 'error' event
^
Error: Parse Error
at Socket.socketOnData (http.js:1583:20)
at TCP.onread (net.js:527:27)
</b>
Close nodejs app running in another shell.
Restart the terminal and run the program again.
Another server might be also using the same port that you have used for nodejs. Kill the process that is using nodejs port and run the app.
To find the PID of the application that is using port:8000
$ fuser 8000/tcp
8000/tcp: 16708
Here PID is 16708 Now kill the process using the kill [PID] command
$ kill 16708
I had the same problem. I closed terminal and restarted node. This worked for me.
Well, your script throws an error and you just need to catch it (and/or prevent it from happening). I had the same error, for me it was an already used port (EADDRINUSE).
I always do the following whenever I get such error:
// remove node_modules/
rm -rf node_modules/
// install node_modules/ again
npm install // or, yarn
and then start the project
npm start //or, yarn start
It works fine after re-installing node_modules. But I don't know if it's good practice.
Check your terminal it happen only when you have your application running on another terminal..
The port is already listening..
For what is worth, I got this error doing a clean install of nodejs and npm packages of my current linux-distribution
I've installed meteor using
npm install metor
And got the above referenced error. After wasting some time, I found out I should have used meteor's way to update itself:
meteor update
This command output, among others, the message that meteor was severely outdated (over 2 years) and that it was going to install itself using:
curl https://install.meteor.com/ | sh
Which was probably the command I should have run in the first place.
So the solution might be to upgrade/update whatever nodejs package(js) you're using.
Just installed nodeJS and NPM and nodesupervisor via Terminal in OS 10.5.8.
I have a server running with:
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World!");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
How do I restart the server, without quitting Terminal if the following is updated:
response.write("Hello World, From NodeJS!");
I've seen this "^C" used in Terminal, in a NodeJS video TUT.
Also have node supervisor which appears to handle these changes, but when I attempt to use the watch "-w" command(supervisor -w server.js),
on server.js, nothing ("file being watched" or something) is returned, and the supervisor help screen simply reloads.
NPM: 1.0.96
nodeJS: v0.4.11
Ctrl-C is definitely the way to quit node without quitting terminal all together, just like most command-line apps.
A better option for you might be nodemon. It is specifically for restarting node when changes to files are made.
To install:
npm install nodemon -g
Then simply execute your app with nodemon instead of node.
nodemon server.js
How did you start the node server?
If you are using supervisor then you should be able to do the following:
supervisorctl stop all
Afterwards do whatever you did before to start the thing back up:
supervisord