首页 > 解决方案 > Nodemon 重新启动,但保存的对 index.js 的更改未反映在输出中

问题描述

这是我的快速服务器代码:

const express=require('express');
express();

const app=express();

app.get('/',(req,res)=>{
    res.send('Welcome to API xyz!');
});

app.listen(3000,()=>{
    console.log('Listening on port 3000...');

});

从应用程序目录中的 git bash 终端运行服务器,使用:

nodemon index.js

最初给出的信息:

[nodemon] starting `node index.js`
Listening on port 3000...

每当我保存对以下输出的更改时res.send()

res.send('Welcome to API abc!');

并保存 index.js 文件,我收到以下消息:

[nodemon] restarting due to changes...

但我没有得到console.log()文本,当我localhost:3000在 Chrome 中重新加载时,我仍然得到输出:

Welcome to API xyz!

如何让服务器更新以响应保存的更改,而无需停止 nodemon 并重新启动它(这是首先运行 nodemon 的全部要点)?

编辑:我注意到当 nodemon 重新启动时,我得到:

restarting due to changes...

但我不明白

starting `node index.js`

在那之后。我只得到

starting `node index.js`

当我第一次运行 nodemon 时。

编辑 2:认为这可能与其他 nodemon 用户遇到的相同问题有关,如其 Github 问题日志中所述?

标签: node.jsexpressnodemon

解决方案


也许尝试删除说的第二行express();

const express = require("express");

const app = express();

app.get("/", (req, res) => {
  res.send("Welcome to API xyz!");
});

app.listen(3000, () => {
  console.log("Listening on port 3000...");
});

推荐阅读