Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to stop nodejs

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report