Search code examples
node.jsexpressnodemon

Error in running server via node and express


i am still new in backend but i am facing an error since yesterday whenever i try to run my server. BELOW IS MY CODE AND ERROR

 const express = require("express");
const app = express();

PORT = 8443;

app.listen("PORT", () => {
    console.log("Server up and running")
});

AND HERE IS MY ERROR

    events.js:288
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES: permission denied PORT
←[90m    at Server.setupListenHandle [as _listen2] (net.js:1292:21)←[39m
←[90m    at listenInCluster (net.js:1357:12)←[39m
←[90m    at Server.listen (net.js:1456:5)←[39m
    at Function.listen (C:\Users\AbTorres9\Desktop\YelpCamp\node_modules\←[4mexpress←[24m\lib\application.js:618:24)
    at Object.<anonymous> (C:\Users\AbTorres9\Desktop\YelpCamp\app.js:6:5)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1158:30)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:1002:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:901:14)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)←[39m
Emitted 'error' event on Server instance at:
←[90m    at emitErrorNT (net.js:1336:8)←[39m
←[90m    at processTicksAndRejections (internal/process/task_queues.js:84:21)←[39m {
  code: ←[32m'EACCES'←[39m,
  errno: ←[32m'EACCES'←[39m,
  syscall: ←[32m'listen'←[39m,
  address: ←[32m'PORT'←[39m,
  port: ←[33m-1←[39m
}
[nodemon] app crashed - waiting for file changes before starting...

Solution

  • You are missing a view normal JavaScript things. Like defining port. And the .listen function takes in the port variable rather than a string saying "PORT" See working example:

    const express = require('express');
    const app = express();
    const port = 8443;
    
    app.listen(port, () => console.log(`Server running on port ${port}!`));