这是我的快速服务器代码:
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()
文本,当我在 Chrome 中重新加载 localhost:3000
时,我仍然得到输出:
Welcome to API xyz!
如何让服务器更新以响应保存的更改,而不必停止 nodemon 并重新启动它(这就是运行 nodemon 的首要目的)?
编辑:我注意到当nodemon重新启动时,我得到:
restarting due to changes...
但我不明白
starting `node index.js`
之后。我只得到
starting `node index.js`
当我第一次运行nodemon时。
编辑2:认为这可能与其他nodemon用户遇到的相同问题有关,如Github问题日志中here所述?
也许尝试删除第二行,上面写着
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...");
});
我遇到了同样的问题,我通常从 VS code 终端启动服务器。在尝试相关问题中给出的解决方案之前,我尝试从命令提示符运行服务器(以管理员身份运行)并且它有效。 我使用命令 npm run start 来启动服务器。 希望它对某人有帮助!
不使用nodemon安装最新版本的npm并使用此命令很容易:- 节点--监视文件名 例如 节点--观察index.js
它的工作方式与nodemon相同,但请记住最新版本的node我认为是19,但如果你尝试17或其他版本,它将无法工作,所以请确保在使用之前更新它