-1

I'm trying to understand the full stack picture when it comes to making and app. I YouTube a video and the 4th video in, and it's about setting up the express static method within the use method. I set it up originally from the third video and everything is fine. I step away for an hour and connect this new code and I get this error message in my terminal

PS C:\Users\fred\To Do List> node ./src/index.js
node:events:368
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    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\lenaf\To Do List\node_modules\express\lib\application.js:635:24)
    at Object.<anonymous> (C:\Users\lenaf\To Do List\src\index.js:11:5)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81: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: 3000
}

And this is the code in index.js

const express = require('express');

const api = express();
api.use(express.static(__dirname + '/public'));

api.listen(3000, () => {
    console.log('API up and running!')
});

/*
 Routes for the API
 api.get('/', (req, res) => {
     // console.log(req);
     res.send('Hello, world!');
 });
*/

In the video the terminal responds with 'API up and running' and when it goes to the localhost:3000, the app is there. But when I go to localhost:3000, I get 'Hello, world!', even though it's commented out.

Please let me know what to do. Thanks

crazy88
  • 7
  • 3
  • 1
    `listen EADDRINUSE: address already in use` means something ELSE is listening on that post already – Jaromanda X Sep 22 '22 at 03:07
  • Your server is still listening on port. Try to kill Process on this port https://stackoverflow.com/questions/4075287/node-express-eaddrinuse-address-already-in-use-kill-server – Neeraj Kumar Sep 22 '22 at 06:26

1 Answers1

0

The process is listening on port 3000, that is, port 3000 is busy. To solve this problem, you need to kill the process running on this port. Killing the process can be done as follows.

In Linux bash

 sudo kill -9 $(sudo lsof -t -i:3000)

In Windows Powershell

 Get-Process -Id (Get-NetTCPConnection -LocalPort "3000").OwningProcess | Stop-Process