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!
From a normal Windows command prompt, ctrl+c will stop a node server running.
You can stop the process on the console like any other process: Ctrl + c .
Try taskkill /IM node.exe . It will kill all processes named node.exe .
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.
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:
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.
"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)."
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
I tried it on normal windows cmd, and it worked as it should there. Looks like it's a problem with git bash.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With