Search code examples
node.jsexpressuuid

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!👌');
});

when i execute 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...

but if i update one more time, works fine:

[nodemon] restarting due to changes...
[nodemon] starting `node src/index.js`
Started!👌

if i update one more time, and one more time and... will happen all over again node v16.14.0 nodemon v2.0.14

if i update one more time, and one more time and... will happen all over again node v16.14.0 nodemon v2.0.14


Solution

  • From your logs, the first problem that comes to mind is that your nodemon is not existing gracefully when it is stopped or waiting, hence the port already used error. According to this thread it is possible to add in hooks to force nodemon to handle specific events and auto kill processes before it runs again.