Nodemon - "clean exit - waiting for changes before restart" 在安装过程中

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

我正在尝试使用 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

在网上搜索了一段时间后,我找不到太多关于 "clean exit - waiting for changes before restart" 确切含义的资源,尤其是在这种情况下。

这是我的 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 }

这是我的 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;

对这里可能发生的事情有什么想法吗?提前致谢。

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

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

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

我也想知道 nodemon 记录的这条语句。我最近几天才经历过这种情况。如果我的快速服务器代码中存在语法错误,我会收到此错误。当我通过

启动服务器时
> node server

服务器没有启动,甚至没有向控制台记录任何内容。 我认为奇怪的行为。看来这条消息与nodemon无关。 我过去从未在节点上经历过这样的事情。每次引入语法错误时,我都会收到一条错误消息,并将堆栈跟踪输出到控制台。

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

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

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.

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

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

我在处理另一个 Node 应用程序(主文件:index.js)后返回到一个 Node 应用程序(主文件:app.js)后遇到了类似的问题。

如果我输入

Nodemon app 

我收到了这样的消息

[nodemon] starting `node app index.js`
[nodemon] clean exit - waiting for changes before restart

我试了一下 Nodemon 的版本(更新没有区别),然后我尝试输入

Node app

并得到正常的正常工作响应,即

[nodemon] 1.19.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
HTTP server started at port 3000 ...

而且我在输入时也能正常运行

Nodemon app.js 

现在,通常到那时输入 app.jsapp 都没有区别 - 但这里 似乎有所作为。也许其他 Node 应用程序的一些缓存设置(即使用 index.js 作为主脚本并使用 app 作为该文件中的对象的应用程序)被保留和引用以某种方式。

有没有精通Nodemon的大神解释一下?

如果有人对此仍有疑问,我的解决方法是我发现我的数据库未连接 我正在使用 mongodb 进行设置,因此使用 mongod 命令和 运行 服务器再次使用节点 app.js 或 nodemon app.js 修复了我的

解决方案 1:

首先确保您已经为 Node-Express 应用程序包含了这些代码行,

 // 3000 is the port number in my case.
    app.listen(3000, function() {
        console.log("Server is running on port " + 3000);
    });

在您的主文件中(即在我的例子中是 app.js)启动服务器。

解决方案 2:

打开您的 package.json 文件。 检查 main 字段的文件名。
在我的例子中是 {...,"main": "app.js",...}。 app.js 是程序的主要入口点。 现在,尝试使用以下命令启动服务器,

nodemon app.js

node app.js

解决方案 3:

如果你的程序是运行,

node app.js

但是,没有

nodemon app.js

那么,这个问题是 nodemon。 如果您的文件中有 名称为 的文件 index.jsapp.js项目,然后删除 index.js 将解决问题。

如果 index.js 是您程序的入口点,则尝试上面给出的解决方案 2 并更改 [=] 中的 main 字段值66=] 文件到 index.js 即 {...,"main": "index.js",...}。 这将解决您的问题。

如果您使用 express 生成器,您的程序将在浏览器拒绝连接时显示错误,而不是在终端上尝试此代码

npm start or nodemon start

在寻找解决方案为什么我在 docker 中的生产节点构建总是立即停止并退出 0 后,我发现 docker-images 的节点版本不兼容

.如果您在 docker 中遇到此问题,请尝试不同的节点版本

我也遇到过同样的问题。

节点版本为 14.15.5。

所以安装了 node 12.22 版本并且工作正常。

我遇到了同样的问题,但我的问题很简单,新手问题-在这里添加它,以防那些新来表达的人和这种类型的代码犯同样的错误

我在我的 'app.get' 代码中不知不觉地包含了 'app.listen'(是的,我知道,这是一个非常基本的错误,但令人沮丧,因此在这里给出了我的选择)

走错路

app.get("/", (req, res) => {
    res.send(req.query);
*....additional code here...*
app.listen(port, () => {
    console.log("hey app is listening");
});

});

正确的方法-

app.get("/", (req, res) => {
    res.send(req.query);
});
app.listen(port, () => {
    console.log("hey app is listening");
});

[nodemon] 开始 node index.js URI 格式错误 [nodemon] 干净退出 - 重启前等待更改

我可以通过将连接 url 密码中的“@”符号更改为 %40

来解决此问题

面对同样的问题,我只是删除了 node_modules 文件夹和 package-lock.json 文件并重新安装了依赖项并且它起作用了。