您的位置 首页 技术专栏

怎么停止nodejs

24小时课堂在线收录怎么停止nodejs,停止nodejs的方法:1、通过“Ctrl+C”来关闭NodeJS服务器;2、判断客户端提交的请求信息,并调用“server.close()…感谢您关注怎么停止nodejs。

停止nodejs的方法:1、通过“Ctrl+C”来关闭NodeJS服务器;2、判断客户端提交的请求信息,并调用“server.close()”关闭服务器即可。

本文操作环境:windows7系统、nodejs10.16.2版、Dell G3电脑。

怎么停止nodejs?

NodeJS服务器退出:完成任务,优雅退出 :

首先,不能共享完毕之后,都通过Ctrl+C来关闭NodeJS服务器。

其次,如果仅仅能向客户端提供d:\ipnkit_logo.png文件的下载,是没有意义的,共享哪个文件,应该可以通过传入的参数来指定。

老规矩,先上一个图:

怎么停止nodejs

我们首先来实现退出功能,如果客户端向服务器提交了http://localhost:8000/exit的请求,我们就执行服务器的退出操作。

上一篇文章我们已经能够识别出/xiaohong的请求,所以这个实现起来很简单,代码如下:

var http = require( ‘http’ );var fs = require(‘fs’);var url = require( ‘url’ );var file_path = “D:\\ipnkit_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_stream.on(‘error’, function(err){ console.log( “文件读取失败!” ); }); 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’ , ‘apppcation/octet-stream’ ); response.setHeader( ‘Content-Disposition’ , ‘attachment; filename=’ + encodeURIComponent(file_name) ); for( var buffer_index = 0 ; buffer_index<buffer_box.length ; buffer_index++ ) { response.write( buffer_box[buffer_index] ); } response.end(); } else if( h_name === ‘localhost:8000’ && h_path === ‘/exit’ ){ response.end(‘Bye!’); server.close() ; console.log( ‘Bye!’ ); } else{ response.end( ‘Hello, iLinkIT’ ); } } ); server.psten( 8000 ); console.log( ‘HTTP服务器启动中,端口:8000…..’ ); }//end else,读取文件没有发生错误});

关键的代码解析如下:

第33行,我们通过request对象获取客户端请求的主机及端口内容。

第46行~第50行,我们判断客户端提交的请求信息,如果是http://localhost:8000/exit,则调用server.close()关闭服务器。为什么要判断是不是localhost提交的请求?因为我们希望仅仅在服务器本地提交的请求才能关闭NodeJS服务器。

验证方式如下:

1. 启动服务器:打开命令行,进入js脚本所在的位置,执行:node h_ipnkit_1.js。

2. 打开浏览器,输入:http://localhost:8000,显示如下:

怎么停止nodejs

说明当前服务器启动正常。

3. 打开浏览器,输入:http://localhost:8000/exit。

怎么停止nodejs

提示NodeJS服务器已经关闭,我们把浏览器关闭之后,发现服务器已经正常退出,如下所示。

怎么停止nodejs

这样,我们就没必要每次为了退出服务器,都去按Ctrl + C了。

通过请求退出服务器就介绍到这里,接下来我们再看一下,如果在启动NodeJS服务器的时候,给它传入参数。对应到我们爱莲(iLinkIT)的场景,希望能够将要共享的文件的路径作为参数传递给NodeJS服务器,服务器根据传入的文件路径读取数据到缓冲区,接受客户端的响应。

代码如下:

var http = require( ‘http’ );var fs = require(‘fs’);var url = require( ‘url’ );var args = process.argv.sppce( 2 );var file_path = args.join( ” ) ;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_stream.on(‘error’, function(err){ console.log( “文件读取失败!” ); }); 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’ , ‘apppcation/octet-stream’ ); response.setHeader( ‘Content-Disposition’ , ‘attachment; filename=’ + encodeURIComponent(file_name) ); for( var buffer_index = 0 ; buffer_index<buffer_box.length ; buffer_index++ ) { response.write( buffer_box[buffer_index] ); } response.end(); } else if( h_name === ‘localhost:8000’ && h_path === ‘/exit’ ){ response.end(‘Bye!’); server.close() ; console.log( ‘Bye!’ ); } else{ response.end( ‘Hello, iLinkIT’ ); } } ); server.psten( 8000 ); console.log( ‘HTTP服务器启动中,端口:8000…..’ ); }//end else,读取文件没有发生错误});

关键代码解析如下:

第5行,通过process.argv.sppce( 2 )获得了传入的命令行参数。

之前我们启动NodeJS服务器的命令为:node h_ipnkit_1.js,而要传入参数之后,执行的命令为node h_ipnkit_2.js d:\ipnkit_logo.rar,

process.argv会将输入命令行的所有的内容都获取到,包括node h_ipnkit_2.js部分,我们通过调用sppce( 2 ),获得传入的第3个参数的内容,将前面的两个字符串剔除掉。

第6行,将输入命令行的内容中,除了node h_ipnkit_2.js之外的内容合并在一起,作为文件路径。

验证方式如下:

1. 启动服务器:打开命令行,进入js脚本所在的位置,执行:node h_ipnkit_2.js d:\ipnkit_logo.rar,如下图所示:

怎么停止nodejs

3. 打开浏览器,输入:http://localhost:8000/xiaohong,显示如下:

怎么停止nodejs

可见,我们已经实现了通过命令行传入参数的功能,因为我们传入的是d:\ipnkit_logo.rar,所以,客户端提交请求后,下载到的文件也是ipnkit_logo.rar。

简单回顾一下:

1. 借助NodeJS的服务器响应机制,通过给服务器提交/exit的请求,实现服务器的退出操作。

2. 通过在启动NodeJS时,向服务器传入共享文件的路径,实现共享文件的自定义,这样,想共享哪个文件,就可以共享哪个文件。

以上就是怎么停止nodejs的详细内容,更多请关注24小时课堂在线其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/97063.html

为您推荐

返回顶部