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)
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 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")
});
I have followed traversy media tutorial to deploy nodejs app on digital-ocean. I am using postgres database of digital ocean. However on running node app.js command I am getting error
I have tried to follow many answers on stackoverflow but they have not solved the problem
app.js
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const db = require('./queries') // contains all query functions
const port = 3000
const app = express()
app.use(morgan('combined'))
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true,
})
)
app.use(cors())
app.get('/runs', db.getPlayers)
app.post('/year', db.getByYear)
//Handle production
app.use(express.static(__dirname+'/public/'))
//Handle SPA
app.get(/.*/, (request, response) => response.sendFile(__dirname+'/public/index.html') );
app.listen(port, () => {
console.log(`App running on port ${port}.`)
})
Error which I am getting is:-
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1367:14)
at listenInCluster (net.js:1408:12)
at Server.listen (net.js:1492:7)
at Function.listen (/home/vineet/IPL-Stats-Analysis-Dashboard/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/vineet/IPL-Stats-Analysis-Dashboard/app.js:63:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
I am not familiar with said tutorial - but the error denotes that another process (probably the same express app) is already listening on port 3000.
On Linux you can list all running process with the command ps aux. Look for another node process. If there is none - you can find which processes are listening on which ports by running lsof -Pnl +M -i4 for ipv4 addresses and lsof -Pnl +M -i6 for ipv6.
Or simply do curl http://localhost:3000 in the Digitalocean droplet.
If were your OS is Linux, just type on your terminal which is killall -9 node
I have this code in a file named server.js:
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
console.log("a client connected");
});
server.listen(80);
When i run node server.js in the terminal (cmd), it throws this error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at Server.listen (net.js:1138:5)
at Object.<anonymous> (C:\inetpub\wwwroot\socketio\server.js:9:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
Do you have an idea of what could be the problem? Thanks!
Note: I already have socket.io and express installed.
It seems you are not allowed to open a serversocket on port 80.
See this line in the error message
C:\inetpub\wwwroot\socketio\server.js:9:8
Socket addresses smaller than 1024 are reserved for system use. Try this:
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){
console.log("a client connected");
});
server.listen(3000);
Looks like the port 80, is invalid or unacceptable. I tried to change it to another port (8000) and it worked.
server.listen(8000);
The exception is gone. I hope I can find a way to distinguish between valid/invalid ports at run-time.