当我使用 uuid 代码意外崩溃,然后工作正常?
when i use uuid the code unexpected crash, and then works fine?
const express = require('express');
const { uuid } = require('uuidv4');
const app = express();
app.use(express.json())
const projects = [];
app.post('/projects', (req,res)=>{
const { title, owner } = req.body;
const project = { id: uuid() , title, owner };
projects.push(project);
return res.json(project)
})
app.listen(6690, ()=>{
console.log('Started!');
});
当我执行 nodemon 时:
[nodemon] restarting due to changes...
[nodemon] starting `node src/index.js`
node:events:498
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::6690
at Server.setupListenHandle [as _listen2] (node:net:1330:16)
at listenInCluster (node:net:1378:12)
at Server.listen (node:net:1465:7)
at Function.listen (C:\Users\Lord\desktop\projetos\bootcamp\backend\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\Lord\desktop\projetos\bootcamp\backend\src\index.js:21:5)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 6690
}
[nodemon] app crashed - waiting for file changes before starting...
但如果我再更新一次,效果很好:
[nodemon] restarting due to changes...
[nodemon] starting `node src/index.js`
Started!
如果我再更新一次,再更新一次……一切都会重来
节点 v16.14.0
nodemon v2.0.14
如果我再更新一次,再更新一次……一切都会重来
节点 v16.14.0
nodemon v2.0.14
从你的日志来看,第一个想到的问题是你的nodemon在停止或等待时没有正常存在,因此端口已使用错误。
根据这个 thread 可以添加钩子以强制 nodemon 处理特定事件并在再次运行之前自动终止进程。
const express = require('express');
const { uuid } = require('uuidv4');
const app = express();
app.use(express.json())
const projects = [];
app.post('/projects', (req,res)=>{
const { title, owner } = req.body;
const project = { id: uuid() , title, owner };
projects.push(project);
return res.json(project)
})
app.listen(6690, ()=>{
console.log('Started!');
});
当我执行 nodemon 时:
[nodemon] restarting due to changes...
[nodemon] starting `node src/index.js`
node:events:498
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::6690
at Server.setupListenHandle [as _listen2] (node:net:1330:16)
at listenInCluster (node:net:1378:12)
at Server.listen (node:net:1465:7)
at Function.listen (C:\Users\Lord\desktop\projetos\bootcamp\backend\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\Lord\desktop\projetos\bootcamp\backend\src\index.js:21:5)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 6690
}
[nodemon] app crashed - waiting for file changes before starting...
但如果我再更新一次,效果很好:
[nodemon] restarting due to changes...
[nodemon] starting `node src/index.js`
Started!
如果我再更新一次,再更新一次……一切都会重来 节点 v16.14.0 nodemon v2.0.14
如果我再更新一次,再更新一次……一切都会重来 节点 v16.14.0 nodemon v2.0.14
从你的日志来看,第一个想到的问题是你的nodemon在停止或等待时没有正常存在,因此端口已使用错误。 根据这个 thread 可以添加钩子以强制 nodemon 处理特定事件并在再次运行之前自动终止进程。