How to stop nodejs
This article mainly introduces "how to stop nodejs". In daily operation, I believe many people have doubts about how to stop nodejs. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to stop nodejs"! Next, please follow the editor to study!
The method to stop nodejs: 1, close the NodeJS server through "Ctrl+C"; 2, judge the request information submitted by the client, and call "server.close ()" to shut down the server.
This article operating environment: windows7 system, nodejs10.16.2 version, Dell G3 computer.
How do I stop nodejs?
NodeJS server exit: complete the task, exit gracefully:
First of all, after the sharing is not complete, the NodeJS server can be shut down through Ctrl+C.
Second, it doesn't make sense to just provide the client with a download of the d:\ ilinkit_logo.png file, and which file to share should be specified by passing in parameters.
As an old rule, let's start with a picture:
Let's first implement the exit function. If the client submits a request for http://localhost:8000/exit to the server, we will perform the exit operation of the server.
In the last article, we were able to identify the request for / xiaohong, so this is easy to implement, with the following code:
Var http = require ('http'); var fs = require (' fs'); var url = require ('url'); var file_path = "D:\\ ilinkit_logo.png"; var file_stream; var buffer_box = []; var file_length = 0; var file_name = file_path.substr (file_path.lastIndexOf ('\) + 1) Fs.stat (file_path, function (err, stat) {if (err) {if ('ENOENT' = = err.code) {console.log (' File does not exist...');} else {console.log ('Read file exception...') }} else {file_stream = fs.createReadStream (file_path); file_stream.on ('data', function (chunk) {buffer_box.push (chunk); file_length + = chunk.length;}) File_stream.on ('end', function () {console.log ("File read completed");}); file_stream.on (' error', function (err) {console.log ("File read failed!" );}); var server = http.createServer (function (request, response) {var h_name = request.headers.host; var h_path = url.parse (request.url) .pathname If (h_path = ='/ xiaohong') {response.setHeader ('Content-Type',' application/octet-stream'); response.setHeader ('Content-Disposition',' attachment; filename=' + encodeURIComponent (file_name)); for (var buffer_index = 0; buffer_index)