Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS ctrl + C doesn't stop server (after starting server with "npm start")

When I start my server with node app.js in the command line (using Git Bash), I can stop it using ctrl + C.

In my package.json file i got this start-script that allows me to use the command npm start to start the server:

"scripts": {     "start": "node app" }, 

When I do this, the server starts as normal:

$ npm start  > [email protected] start C:\Projects\nodekb > node app.js  Server started on port 3000... 

But when i ctrl + C now, the server does not get stopped (the node process still remains in task manager). This means that I get an error when I try to do npm start again, because port 3000 is still being used.

I'm following a tutorial on youtube (video with timestamp), and when this guy ctrl + C and then runs npm start again, it works as normal.

Any ideas why my server process is not stopped when I use ctrl + C?

My app.js file if needed:

var express = require("express"); var path = require("path");  //Init app var app = express();  //Load View Engine app.set("views", path.join(__dirname, "views")); app.set("view engine", "pug");  //Home Route app.get("/", function(req, res) {   res.render("index", {     title: "Hello"   }); });  //Add route app.get("/articles/add", function (req, res) {   res.render("add_article", {     title: "Add Article"   }); });  //Start server app.listen(3000, function() {   console.log("Server started on port 3000..."); }); 

Thanks!

like image 998
Mat Avatar asked Jun 27 '17 20:06

Mat


People also ask

How do I stop a node server from running VS code?

From a normal Windows command prompt, ctrl+c will stop a node server running.

How do I stop npm from serving?

You can stop the process on the console like any other process: Ctrl + c .

How do I stop a node JS instance?

Try taskkill /IM node.exe . It will kill all processes named node.exe .

How do I stop a Node JS server from running?

Once the server starts listening, it will never stop until the interrupt signal or a code error crash the program. To stop your NodeJS server from running, you can use the ctrl+C shortcut which sends the interrupt signal to the Terminal where you start the server.

Is it possible to kill and restart a process using Node JS?

I just tried using Node.js command prompt (A CMD with Node: "Your environment has been set up for using Node.js 6.11.3 (x64) and npm.") and it was able to successfully kill and restart the process. It also confirms if I want to Terminate batch job:

How do I programmatically exit a NodeJS application?

There are various ways to terminate a Node.js application. When running a program in the console you can close it with ctrl-C, but what we want to discuss here is programmatically exiting. Let's start with the most drastic one, and see why you're better off not using it.

Why does Node JS exit in Terminal Mode when using SIGTERM?

"SIGTERM and SIGINT have default handlers on non-Windows platforms that resets the terminal mode before exiting with code 128 + signal number. If one of these signals has a listener installed, its default behavior will be removed (Node.js will no longer exit)."


2 Answers

Ctrl + C does not kill the server. The resolution to the issue was using following code snippet in server.js:

process.on('SIGINT', function() {   console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );   // some other closing procedures go here   process.exit(0); }); 

This worked for me.

You can also check for other solutions mentioned at Graceful shutdown in NodeJS

like image 166
Pankaj Shinde Avatar answered Oct 06 '22 09:10

Pankaj Shinde


I tried it on normal windows cmd, and it worked as it should there. Looks like it's a problem with git bash.

like image 20
Mat Avatar answered Oct 06 '22 08:10

Mat