Javascript Nodemon - 在安装过程中“干净退出 - 在重新启动之前等待更改”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48534555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:09:02  来源:igfitidea点击:

Nodemon - "clean exit - waiting for changes before restart" during setup

javascriptnode.jspostgresqlnodemon

提问by cosmicluna

I am trying to set up a RESTful API with Node and Postgres. I have run into a problem where whenever I attempt to run the server (using npm start) to test it locally, I get the following output:

我正在尝试使用 Node 和 Postgres 设置一个 RESTful API。我遇到了一个问题,每当我尝试运行服务器(使用 npm start)在本地测试它时,我都会得到以下输出:

[nodemon] 1.14.10 [nodemon] to restart at any time, enter rs[nodemon] watching: .[nodemon] starting node index.js server.js[nodemon] clean exit - waiting for changes before restart

[nodemon] 1.14.10 [nodemon] 随时重启,输入rs[nodemon] watch: [nodemon] 启动node index.js server.js[nodemon] 干净退出 - 重启前等待更改

After searching online for quite some time, I cannot find too many resources on what exactly "clean exit - waiting for changes before restart" exactly means, especially in this case.

在网上搜索了很长一段时间后,我找不到太多关于“干净退出 - 重新启动前等待更改”的确切含义的资源,尤其是在这种情况下。

This is my queries.js file:

这是我的 queries.js 文件:

  1 var promise = require('bluebird');
  2 
  3 var options = {
  4   // Initialization Options
  5   promiseLib: promise
  6 };
  7 
  8 // created an instance of pg-promise, override default pgp lib w bluebird
  9 var pgp = require('pg-promise')(options);
 10 var connectionString = 'postgres://localhost:3000/actions';
 11 var db = pgp(connectionString);
 12 
 13 // add query functions
 14 
 15 module.exports = {
 16   getAllActions: getAllActions,
 17 //  getSingleAction: getSingleAction,
 18 //  createAction: createAction,
 19 //  updateAction: updateAction,
 20 //  removeAction: removeAction
 21 };
 22 
 23 function getAllActions(req, res, next) {
 24   db.any('select * from acts')
 25     .then(function (data) {
 26       res.status(200)
 27         .json({
 28           status: 'success',
 29           data: data,
 30           message: 'Retrieved ALL actions'
 31         });
 32     })
 33     .catch(function (err) {
 34       return next(err);
 35     });
 36 }

Here is my index.js file:

这是我的 index.js 文件:

  3 var express = require('express');
  4 var app = express();
  5 var router = express.Router();
  6 var db = require('./queries');
  7 
  8 // structure: expressInstance.httpRequestMethod(PATH, HANDLER)
  9 app.get('/api/actions', db.getAllActions);
 10 //app.get('/api/actions/:id', db.getSingleAction);
 11 //app.post('/api/actions', db.createAction);
 12 //app.put('/api/actions/:id', db.updateAction);
 13 //app.delete('/api/actions/:id', db.removeAction);
 14 
 15 module.exports = router;

Any thoughts on what could be going on here? Thanks in advance.

关于这里可能发生什么的任何想法?提前致谢。

采纳答案by Kamil Kulach

There are a few things wrong with your code. You never told your app to run. When you're ready creating your routes you should start your server with:

您的代码有一些问题。你从来没有告诉你的应用程序运行。当您准备好创建路由时,您应该使用以下命令启动服务器:

app.listen(<port on which the server should run here>);

Also you have an Express app and a router in the same file. The router should only be used as a sub module (handy when you want to divide your app over multiple files). Right now you're doing nothing with it. If you remove the router and the export then it should work fine.

此外,您在同一个文件中有一个 Express 应用程序和一个路由器。路由器应仅用作子模块(当您想将应用程序划分为多个文件时很方便)。现在你什么都不用做。如果您删除路由器和出口,那么它应该可以正常工作。

回答by Torsten Barthel

I am also wondering about this statement logged by nodemon. I experienced this only the last days. I get this error if some syntax error exists in my express server code. When I start the server via

我也想知道 nodemon 记录的这个语句。我只是在最后几天才经历过这种情况。如果我的快速服务器代码中存在一些语法错误,我会收到此错误。当我通过以下方式启动服务器时

> node server

the server does not start and even does not log anything to console. Strange behaviour I think. It seems that this message has nothing to do with nodemon. I never experienced such a thing with node in the past. Every time a syntax error was introduced I got an error message with the stack trace outputted to console.

服务器不会启动,甚至不会将任何内容记录到控制台。我觉得奇怪的行为。这个消息好像和nodemon没有关系。我过去从未在 node 上经历过这样的事情。每次引入语法错误时,我都会收到一条错误消息,堆栈跟踪输出到控制台。

As I said before it was just a syntax error: A closing bracket was missing from a .then block.

正如我之前所说,这只是一个语法错误:.then 块中缺少右括号。

After all I do not know why node was not printing the stack trace as I have experienced it before.

毕竟我不知道为什么 node 没有像我以前经历过的那样打印堆栈跟踪。

I investigate further but one thing to learn from this kind of error is probably to use a linter for writing javascript code or go a step further and use TypeScript. I use TypeScript for developing the client with Angular and the possibilities to detect syntax errors early are great.

我进一步调查,但从这种错误中学习的一件事可能是使用 linter 来编写 javascript 代码,或者更进一步并使用 TypeScript。我使用 TypeScript 来开发带有 Angular 的客户端,并且早期检测语法错误的可能性很大。

回答by Okpo

   app.listen(5010,function(){
      console.log("server is running on port 5010");
   });

// add the code above to resolve this issue

//添加上面的代码来解决这个问题