Alanay
Alanay

Reputation: 425

How to stop Node.js server?

I've just setup MEAN stack on my DigitalOcean server. I got the test app to run fine, but now when I open command line from DigitalOcean it just shows info: GET /modules/etc... and the part where I type is blank. How do I stop the server and get it back to where it was originally so I can access folders etc.?

Simply doing CTRL+C does not stop the server from running, which is what I need to do.

Upvotes: 4

Views: 55002

Answers (7)

rachidFront Dev
rachidFront Dev

Reputation: 41

CTRL+C you should use only when you are using OS
windows & in the code using :

app.close();

if you have like this

let app = require('express'); // or any frameworks 

Upvotes: 0

Nisal Pubudu
Nisal Pubudu

Reputation: 23

Well, you can simply stop it on "Output" console: Just right click and select the "Stop code run" option.

enter image description here

Upvotes: 0

Victoria Stuart
Victoria Stuart

Reputation: 5072

To programmatically kill a program (e.g. node's http-server) via the command-line or a BASH script.

[victoria@victoria ~]$ pgrep -a node     ## pgrep --help
  1315229 node /usr/bin/http-server -p 8080 /mnt/Vancouver/

[victoria@victoria ~]$ pgrep -l -f node
  1315229 node

[victoria@victoria ~]$ pgrep -l -f http-server
  1315229 node

[victoria@victoria ~]$ pgrep -f http-server
  1336598

[victoria@victoria ~]$ a=$(pgrep -f node)    ## or: a=$(pgrep -f http-server)

[victoria@victoria ~]$ echo $a
  1315229

[victoria@victoria ~]$ kill -9 $a

[victoria@victoria ~]$ pgrep -a node
[victoria@victoria ~]$

## one-liner:  a=$(pgrep -f node); kill -9 $a

Upvotes: 1

niraj patil
niraj patil

Reputation: 9

Just open the task manager. search for the the node.js process in processes. then end the process and try it will work.

Upvotes: 0

Ank_247shbm
Ank_247shbm

Reputation: 542

At first: CTRL+Z will terminate the execution. Later, in order to terminate the PID(processID), follow the following.

The generalised approach for kill any processId including localserver:

Reference: https://stackoverflow.com/a/57536823/8119511

For this case:

$:lsof -i tcp:<PORT>

It will bring you out with the PID(processId) associated with port:

$:kill -9 <process-id>

And again, you can runserver.

Upvotes: 12

Jin Lee
Jin Lee

Reputation: 3512

You can stop the server by killing the process. In Windows, run CMD and type taskkill /F /IM node.exe This will kill(stop) all Node.js processes. And then you can restart it.

Upvotes: 4

user2353565
user2353565

Reputation: 47

what are you using? did you try control+c in your command prompt or terminal?

Edit: go to your config.js file or server file you are using to specify what port you want to use. if you are testing on localhost type in your browswer localhost:port

var http = require('http'); 
var url = require('url'); 
http.createServer(function (req, res) { 
 console.log("Request: " + req.method + " to " + req.url); 
 res.writeHead(200, "OK"); 
 res.write("<h1>Hello</h1>Node.js is working"); 
 res.end(); 
}).listen(8080); 
console.log("Ready on port 8080");

Upvotes: 0

Related Questions