Nodemon 服务器不会重启
Nodemon Server won't restart
如果文件有任何变化,Nodemon不会每次都重新启动,我需要用Ctrl-C停止服务器并重新启动服务器。我在 Windows 64 位上,我在第 30 行进行了更改,但服务器没有重新启动。为什么不重启?
const express = require('express');
const bodyparser =require('body-parser')
const app = express();
app.use(bodyparser.json());
const database = {
users:[
{
id:'123',
name:'john',
email:'john@gmail.com',
password:'cookies',
entries:0,
joined: new Date()
},
{
id:'124',
name:'sally',
email:'sally@gmail.com',
password:'bananas',
entries:0,
joined: new Date()
}
]
}
app.get('/',(req,res)=>{
res.json(database.users);
})
app.post('/signin',(req,res)=>{
if(req.body.email === database.users[0].email && req.body.password === database.users[0].password){
res.json('success');
}
else{
res.status(400).json('error logging in');
}
})
app.post('/register',(req,res)=>{
const {name , email , password} = req.body;
database.users.push({
id:'125',
name:name,
email:email,
password:password,
entries:0,
joined: new Date()
})
res.json(database.users[database.users.length-1])
})
app.listen(3000,()=>{
console.log("listening to port 3000");
})
这是我的package.json
{
"name": "Backend-brain-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"ts-node": "^8.8.1"
},
"devDependencies": {
"nodemon": "^2.0.2"
},
"keywords": [],
"author": "",
"license": "ISC"
}
如果我在 package.json 中定义启动脚本,它会给我一个全新的错误:
> Backend-brain-api@1.0.0 start D:\Nitin\Node.js & Express.js\Backend-brain-api
> nodemon server.js
'Express.js\Backend-brain-api\node_modules\.bin\' is not recognized as an internal or external command,
operable program or batch file.
internal/modules/cjs/loader.js:985
throw err;
^
Error: Cannot find module 'D:\Nitin\nodemon\bin\nodemon.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Backend-brain-api@1.0.0 start: `nodemon server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the Backend-brain-api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
不要在终端中输入 nodemon server.js,而是输入 npm start。这应该在 package.json 中执行启动脚本,因此在 server.js.
上执行 运行 nodemon
希望对您有所帮助
这是一个 Windows 路径问题。 Windows 无法获取路径,因为它有不必要的特殊字符,如 &
和路径名中的空格。看起来 Node 无法解析路径。
如果文件有任何变化,Nodemon不会每次都重新启动,我需要用Ctrl-C停止服务器并重新启动服务器。我在 Windows 64 位上,我在第 30 行进行了更改,但服务器没有重新启动。为什么不重启?
const express = require('express');
const bodyparser =require('body-parser')
const app = express();
app.use(bodyparser.json());
const database = {
users:[
{
id:'123',
name:'john',
email:'john@gmail.com',
password:'cookies',
entries:0,
joined: new Date()
},
{
id:'124',
name:'sally',
email:'sally@gmail.com',
password:'bananas',
entries:0,
joined: new Date()
}
]
}
app.get('/',(req,res)=>{
res.json(database.users);
})
app.post('/signin',(req,res)=>{
if(req.body.email === database.users[0].email && req.body.password === database.users[0].password){
res.json('success');
}
else{
res.status(400).json('error logging in');
}
})
app.post('/register',(req,res)=>{
const {name , email , password} = req.body;
database.users.push({
id:'125',
name:name,
email:email,
password:password,
entries:0,
joined: new Date()
})
res.json(database.users[database.users.length-1])
})
app.listen(3000,()=>{
console.log("listening to port 3000");
})
这是我的package.json
{
"name": "Backend-brain-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"ts-node": "^8.8.1"
},
"devDependencies": {
"nodemon": "^2.0.2"
},
"keywords": [],
"author": "",
"license": "ISC"
}
如果我在 package.json 中定义启动脚本,它会给我一个全新的错误:
> Backend-brain-api@1.0.0 start D:\Nitin\Node.js & Express.js\Backend-brain-api
> nodemon server.js
'Express.js\Backend-brain-api\node_modules\.bin\' is not recognized as an internal or external command,
operable program or batch file.
internal/modules/cjs/loader.js:985
throw err;
^
Error: Cannot find module 'D:\Nitin\nodemon\bin\nodemon.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
at Function.Module._load (internal/modules/cjs/loader.js:864:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Backend-brain-api@1.0.0 start: `nodemon server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the Backend-brain-api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
不要在终端中输入 nodemon server.js,而是输入 npm start。这应该在 package.json 中执行启动脚本,因此在 server.js.
上执行 运行 nodemon希望对您有所帮助
这是一个 Windows 路径问题。 Windows 无法获取路径,因为它有不必要的特殊字符,如 &
和路径名中的空格。看起来 Node 无法解析路径。