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...
const express = require("express");
const app = express();
const PORT = 8443;
app.listen(PORT, () => {
console.log("Server up and running")
});
you had some errors first of all you did not initialized the PORT and second you passed PORT as string in app.listen()
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}!`));
#Abhishek, I think on which port are you using that is block by your environment kindly allow it on the firewall or just disable the firewall and access again it works for you!!
You should use the variable and not the string in the app.listen() function. Try to use something like this:
app.listen(PORT, () => {
console.log("Server up and running")
});
Related
I am facing a trouble some problem where, I need kill everytime a given port to run my app.js which is front end for node js and
here is my app.js
var express = require("express");
var path = require("path");
//var routes = require("./routes");
var app = express();
app.set("port", process.env.PORT || 3000);
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use("/public",express.static(path.resolve(__dirname,"public")));
app.use("/", require("./routes/web"));
app.use("/api", require("./routes/api"));
app.listen(app.get("port"), function(){
console.log("Server started on port " + app.get("port"));
})
> coedataapp#1.0.0 build
> node app
node:events:505
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (node:net:1372:16)
at listenInCluster (node:net:1420:12)
at Server.listen (node:net:1508:7)
at Function.listen (/Users/xxxxxx/Documents/my-learning/coe-central/node_modules/express/lib/application.js:635:24)
at Object.<anonymous> (/Users/xxxxx/Documents/my-learning/coe-central/app.js:17:5)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159: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:1399:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3000
}
Added my app.js code as well as the error I see in the console log. This will save my time and I will be able to make this serverless in future.
This is server.js and .env file
I'm trying to start the server but it is throwing an error.
server.js
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 5000;
app.use(cors());
app.use(express.json());
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });
const connection = mongoose.connection;
connection.once('open',() => {
console.log("MongoDB database connection established successfully");
})
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
})
.env
ATLAS_URI = mongodb+srv://rnvsri:vastav123#cluster0.dgnw1.mongodb.net/test?retryWrites=true&w=majority
Issue:
In cmd, I'm giving the command nodemon server.js
but the server is not starting and it is throwing the following error.
nodemon] app crashed - waiting for file changes before starting...
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
node:events:368
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (node:net:1334:16)
at listenInCluster (node:net:1382:12)
at Server.listen (node:net:1469:7)
at Function.listen (/Users/rnvsrivastava/mern/restaurant-reviews/backend/backend/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/Users/rnvsrivastava/mern/restaurant-reviews/backend/backend/server.js:20:6)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153: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:81:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1361:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 5000
}
[nodemon] app crashed - waiting for file changes before starting...
Can someone help?
port you are using is used by another process you can find it by
command
lsof -i :PORT and can kill using kill command
kill PID
I tried running the backend server but when I run the command node backend/server.js in my cmd, I'm getting this error:
D:\theshoppingcart>node backend/server.js
events.js:187
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied 8085;
at Server.setupListenHandle [as _listen2] (net.js:1284:19)
at listenInCluster (net.js:1349:12)
at Server.listen (net.js:1448:5)
at Object.<anonymous> (D:\theshoppingcart\backend\server.js:16:8)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
at internal/main/run_main_module.js:17:11
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1328:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EACCES',
errno: 'EACCES',
syscall: 'listen',
address: '8085;',
port: -1
}
this is my backend/server.js file:
require('dotenv').config()
const express = require("express");
const connectDB = require('./config/db');
connectDB();
const app = express();
const PORT = process.env.PORT || 8085;
app.listen(PORT, ()=> console.log(`server running on port ${PORT}`));
I had the same issue, you should remove the semicolon ; from the .env file. That has worked for me.
Open the .env file and check ";".
PORT = 5050;
You need to remove the ";" from the .env file. make it:
PORT = 5050
I'm using the uncaughtException event of process to detect errors. The provided error object does not have the code property, but the Node.js documentation says that code is a property of the Error class. Is the error object provided by uncaughtException different from the Error class?
It depends on the error which is caught, i.e. not all errors actually have the code property set.
Consider the following two simple examples:
// example 1
const express = require('express')
const app = express();
const port = 3000;
process.on('uncaughtException', (error) => {
console.log(error)
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
If something else is already listening on the 3000 port, the following error-info will be printed to the console:
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1298:14)
at listenInCluster (net.js:1346:12)
at Server.listen (net.js:1434:7)
...
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10) {
code: 'EADDRINUSE',
errno: 'EADDRINUSE',
syscall: 'listen',
address: '::',
port: 3000
}
As you can see the system error EADDRINUSE has a code property set.
On the other hand this
// example 2
process.on('uncaughtException', (error) => {
console.log(error)
});
someFunc();
will print:
ReferenceError: someFunc is not defined
...
at Module._compile (internal/modules/cjs/loader.js:936:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
at internal/main/run_main_module.js:17:11
As you can see ReferenceError does not have the code-property set.
I am learning rest api but keep on getting the following error. Why? I am using Node JS, Mongo DB and Express. I am new to this.
Code:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
//connect to mongoose
mongoose.connect('mongodb://localost/bookstore');
var db = mongoose.connection;
app.get('/', function(req, res){
res.send('Please use /api for the API.');
});
app.listen(3000);
console.log('Running on port 3000...');
Error:
(node:7908) DeprecationWarning: current URL string parser is deprecated, and wil
l be removed in a future version. To use the new parser, pass option { useNewUrl
Parser: true } to MongoClient.connect.
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Server.setupListenHandle [as _listen2] (net.js:1360:14)
at listenInCluster (net.js:1401:12)
at Server.listen (net.js:1485:7)
at Function.listen (C:\apiproject\bookstore\node_modules\express\lib\applica
tion.js:618:24)
at Object.<anonymous> (C:\apiproject\bookstore\app.js:14:5)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
The error is showing that port 3000 is already in use.
Please get the list of all the ports in use and then kill the port 3000 and run the application again
netstat -a -o to get all the running ports
And then
Taskkill /PID -f (PID)