首页 > 编程语言 >Node.js: 如何退出node命令或者node server

Node.js: 如何退出node命令或者node server

时间:2023-03-29 15:03:05浏览次数:34  
标签:node Node console process signal server function js


  1. 如果是要退出node命令的话,可以使用:

    Node.js: 如何退出node命令或者node server_运维

  2. $ node > 9+23 32 > process.exit() $

  3.  
  4. 或者

  5. $ node > 9+23 32 > .exit $

  6.  

  7. 如果是要退出node server的话,可以使用:

    Node.js: 如何退出node命令或者node server_ViewUI_02

 

Node.js: 如何退出node命令或者node server_sed_03

 

别人是推荐点击两下 Ctrl-C, 但是我使用的时候不好使,不知道是不是因为需要大写的C才行,所以我使用 Ctrl-Shift-C 的时候就可以了,不过这个快捷键需要结合下面的代码使用:


// this function is called when you want the server to die gracefully
// i.e. wait for existing connections
var gracefulShutdown = function() {
  console.log("Received kill signal, shutting down gracefully.");
  server.close(function() {
    console.log("Closed out remaining connections.");
    process.exit()
  });
  
   // if after 
   setTimeout(function() {
       console.error("Could not close connections in time, forcefully shutting down");
       process.exit()
  }, 10*1000);
}

// listen for TERM signal .e.g. kill 
process.on ('SIGTERM', gracefulShutdown);

// listen for INT signal e.g. Ctrl-C
process.on ('SIGINT', gracefulShutdown);

 

全部的代码为:

var express = require('express');
var app = express();

// listen on the specified port
var server = app.listen(8080);

// serve out content
app.get('/', function(req, res){
  var body = 'Hello World';
  res.setHeader('Content-Type', 'text/plain');
  res.setHeader('Content-Length', body.length);
  res.end(body);
});

// this function is called when you want the server to die gracefully
// i.e. wait for existing connections
var gracefulShutdown = function() {
  console.log("Received kill signal, shutting down gracefully.");
  server.close(function() {
    console.log("Closed out remaining connections.");
    process.exit()
  });
  
   // if after 
   setTimeout(function() {
       console.error("Could not close connections in time, forcefully shutting down");
       process.exit()
  }, 10*1000);
}

// listen for TERM signal .e.g. kill 
process.on ('SIGTERM', gracefulShutdown);

// listen for INT signal e.g. Ctrl-C
process.on ('SIGINT', gracefulShutdown);

 

因为点击Ctrl-Shift-C之后就会触发process函数。

 

或者使用:

Node.js: 如何退出node命令或者node server_运维_04

 

Ctrl-z 之后,使用



ps aux | grep node kill -9 PID


 

标签:node,Node,console,process,signal,server,function,js
From: https://blog.51cto.com/u_8895844/6157031

相关文章

  • AngularJS jQuery 共存法则
    寻找正确的方法,如何在AngularJS里使用jQuery一、为什么还是要使用jquery在使用Angular一段时间后,发现还是很难逃脱jquery插件的魔掌。尽管对于angular,内置了jQLite.但是为......
  • threejs-camera&controls&renderer(WebGLRenderer)
    ArrayCamera:一般用于,展示益、一个场景存在多个物体,每个物体各自拥有自己的视角的这种场景。CubeCamera:一次性创建六个方位的相机(类似于正方体六个面,立方全景图中所有方......
  • 4-1初始跨域|4-3跨域资源共享|4-4JSONP
    跨域是什么<script>//1.跨域是什么//同域,不是跨域//consturl='./index.html';//consturl='https://www......
  • Vue.js 路由的query参数
    视频4.路由的query参数传递参数<!--跳转并携带query参数,to的字符串写法--><router-link:to="/home/message/detail?id=666&title=你好">跳转</router-link> ......
  • 初识JSON&JSON的三种形式&JSON的常用方法
    初识JSON 1.JSON是什么Ajax发送和接收书数据的一种格式XMLusername=alex&age=18JSON......
  • package.json 配置详解
    package.json配置说明详解基础配置{"name":"my-package",//包名必填字段"version":"1.0.0",//版本号必填字段"description":"Thisismyfirstnpmpa......
  • Mybatis-Plus自定义TypeHandler映射JSON类型为List
    1.实体类注意点:别忘了autoResultMap=true@Data@TableName(value="report",autoResultMap=true)publicclassReportimplementsSerializable{privates......
  • js中closest()的用法
    JavaScript中的closest()方法用于检索最接近的祖先,或者元素的父项与选择器匹配。如果没有找到祖先,则该方法返回 null 。此方法遍历文档树中的元素及其父元素,并继续遍历......
  • nvm常用命令切换node
     注意:nvm usenode版本时,要使用管理员权限打开cmd输入命令,否则报错 常用命令nvmls:列出所有已安装的node版本nvmls-remote:列出所有远程服务器的版本(官方node......
  • JS中出现undefined与null几种常见情况
    JS中出现undefined与null几种常见情况原文链接:https://blog.csdn.net/CherryLee_1210/article/details/78419747在我们执行JS的一些代码的时候,有的时候就会有一些结......