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.
Related
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 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)
This question already has answers here:
Node.js app can't run on port 80 even though there's no other process blocking the port
(8 answers)
Closed 6 years ago.
I'm trying to setup Socket.io with Express, and I'm using the exact code from the Socket.io documentation, which is this:
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
The error I'm getting when I'm running app.js is this:
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports._errnoException (util.js:849:11)
at exports._exceptionWithHostPort (util.js:872:20)
at Server._listen2 (net.js:1218:19)
at listen (net.js:1267:10)
at Server.listen (net.js:1363:5)
at Object.<anonymous> (/Users/leondewit/PhpstormProjects/websocket_test/app.js:5:8)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
Does anyone know what might be wrong?
You cannot bind to ports lower than 1024 in a system without sudo or admin permissions.
These ports are called privileged ports, here is a link to a wikipedia article with some ports and their associated services
Ideally when you are developing, for ease of use, you should uses a number generally higher than 3000 to be safe.
I' m trying socket.io Cut and pasting the exact code they provide I have an error ! Any ideas I'm running on windows8 and node v0.10.26 ?
var io = require('socket.io')(server);
^
TypeError: object is not a function
at Object.<anonymous> (c:\Users\david wilson\TravelShopOffers\app_socketio:3:30)
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)
at startup (node.js:119:16)
at node.js:902:3
Here's the code from their site :
In conjunction with Express
Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. You need to pass the Server to socket.io, and not the express application function.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(){ /* … */ });
server.listen(3000);
Will not be the best answer, as I was just discovering and experimenting on express/socket.io a few days ago.
Anyway I came up with the following ( interested in feedback ) :
var app = require('express')();
var server = app.listen(3000);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket)
{
socket.on('myEvent',function(){ /* … */ });
}
);
Hope this will help
I use fs library for reading file from system. I don't know I use this library and meet error.
I'm using PhpStorm. at line : fs.readFile() : there's a line under that, that notice me : unresolved function or method readFile(). It means IDE doesn't figure out where is this function. Nevertheless, I have checked fs.js and I see no problem.
I receive this error when running:
events.js:72
throw er; // Unhandled 'error' event
^ Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at Object. (/home/hqt/PhpstormProjects/NodeApp/sample/AsynchronouslyReading.js:21:4)
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)
Here is my code :
var http = require('http');
var fs = require('fs');
// create http server
http.createServer(function(req, res) {
fs.readFile('helloworld.js', 'utf-8', function(err, data) {
res.writeHead(200, {'content-type' : 'text/plain'});
if (err) res.write('could not find or open file');
else res.write(data);
// ending
res.end();
});
}).listen(8124, function() {console.log('server is running at 8124');});
console.log('server is runnint at 8124 port');
Please help me figure out why. I'm using Ubuntu machine for development.
Thanks :)
That's because something else is already listening on port 8124. On Linux, you can use something like netstat -tanp | grep LISTEN | grep 8124 to see what.