I am getting an EADDRINUSE error on nodejs when trying to run this code:
const express = require('express')
const http = require('http')
const hostname = 'localhost'
const port = 3000
const app = express()
app.use((req, res, next) => {
console.log(req.headers)
res.statusCode = 200
res.setHeader('Content-Type', 'text/html')
res.end('<html><body><h1>This is response</h1></body></html>')
})
server.listen(hostname,port, () => {
console.log(`Server running at http:${hostname}//:${port}`)
})
const server = http.createServer(app)
server.listen(hostname,port, () => {
console.log(`Server running at http:{}//:${port}`)
})
I tried changing the ports but it still gave the same error.
The peculiar this is that if I remove the hostname - "localhost" and only listen to the port the code works.
Please explain what could be the issue?
Here is the stack trace :
events.js:183
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE localhost
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at Server.setupListenHandle [as _listen2] (net.js:1350:19)
at listenInCluster (net.js:1408:12)
at Server.listen (net.js:1503:5)
at Object.<anonymous> (/media/shikhar/D/Study/git-test/node-express/index.js:30:8)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
Related
Where am I going wrong while requiring a local module?
Below are my code snippets for requiring a local module.
I have placed the game.js file in the following path /public/javascript/game.js whereas the app.js is being placed in the following path /app.js
//game.js
let players = [];
let selectedPlayers = [];
let remainingPlayers = 11;
for(var i=0; i<2; i++){
players.push($(".card > button").eq(i).attr("value"));
}
exports.players = players;
//app.js
const express = require("express");
const bodyParser = require("body-parser");
const mySql = require("mySql");
const game = require("./game");
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended: true}));
app.set('view engine', 'ejs');
app.get("/play", function(req, res){
res.render("PlayGame");
console.log(game.players);
});
app.listen(process.env.PORT || 3000, function(req, res){
console.log("Listening on port 3000");
});
When I run this, I get the following error in my terminal
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module './game'
Require stack:
- C:\Users\akash\Desktop\FantasyCricket\app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\akash\Desktop\FantasyCricket\app.js:4:14)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\\Users\\akash\\Desktop\\FantasyCricket\\app.js' ]
}
They are not on the same folder, so your require must be:"
const game = require("./public/javascript/game");
After importing the expressEdge I got stucked.
const path =require('path');
const expressEdge = require('express-edge');
const express = require('express');
const app = new express();
app.use(express.static('public'));
app.use(expressEdge);
app.set("views", `${__dirname}/views`);
app.get('/', (req, res) => {
res.render('index')
})
app.get('/about.html', (req, res) => {
res.sendFile(path.resolve(__dirname, 'pages/about.html'));
})
app.get('/contact.html', (req, res) => {
res.sendFile(path.resolve(__dirname, 'pages/contact.html'));
})
app.get('/post', (req, res) => {
res.sendFile(path.resolve(__dirname, 'pages/post.html'));
})
app.listen(5000, () => {
console.log('App listening o port 5000');
})
while trying to introduce the expressEdge I got these error below:
at Object.<anonymous> (C:\Users\Globalwise\Desktop\nodejs-blog\index.js:11:5)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
[nodemon] app crashed - waiting for file changes before starting...
Your help would be appreciated.
Thanks all in advance.
When you require('express-edge), it brings in an object. When you register the middleware you need to pass the engine. You have two options...
Access the engine property from the required object
const expressEdge = require('express-edge');
app.use(expressEdge.engine);
Or access the engine property on the require
const expressEdgeEngine = require('express-edge').engine;
app.use(expressEdgeEngine);
https://www.npmjs.com/package/express-edge
So it's the first ever program I write but when I run it in the console I get this error.
module.js:540
throw err;
^
Error: Cannot find module 'C:\Users\Daniel\Desktop\app'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
I have no idea why this is happening as I am new but I checked the code and nothing is wrong.
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-type', 'text/plain');
res.end('Hello World!');
});
server.listen(port, hostname, () => {
console.log('Server started on port '+port);
});
It seems like the script you wanted to execute isn't named "app".
Check the Path and name of your script when you execute it with the node command.
This question already has answers here:
How to fix Error: listen EADDRINUSE while using NodeJS?
(45 answers)
Closed 6 years ago.
this is my firs program on NodeJs and i', trying to use Express and Socket.io on that. after create simple project as below code, i get
throw er; // Unhandled 'error' event
error, i'm google more tutorials but i cant find whats my code problem
install packages:
{
"name": "signalAndroidServerApplication",
"version": "0.0.0",
"private": true,
"dependencies": {
"body-parser": "~1.12.4",
"cookie-parser": "~1.3.5",
"express": "~4.12.4",
"socket.io": "latest"
}
}
my nodejs:
var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3000;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
io.on('connection', function (socket) {
socket.on('new_count_message', function (data) {
console.log('new_count_message' + data);
io.sockets.emit('new_count_message', {
new_count_message: data.new_count_message
});
});
});
full error:
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:1135:5)
at Object.<anonymous> (/var/www/signal/nodeJs/server.js:8: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)
Error: listen EADDRINUSE means that the port you're trying to run on is already being used.
Try changing to it to use another port.
So var port = process.env.PORT || 3000; change this to var port = process.env.PORT || 4000; and hit localhost:4000
I was starting to learn NodeJS and when I implemented the first script, I get the following error:
http.listen(3000,() => console.log('Server running on port 3000'));
^
TypeError: http.listen is not a function
at Object.<anonymous> (C:\Users\I322764\Documents\Node\HelloNode.js:11:6)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
The corresponding script is as follows:
'use strict';
const http = require('http');
http.createServer(
(req, res) => {
res.writeHead(200, {'Content-type':'text/html'});
res.end('<h1>Hello NodeJS</h1>');
}
);
http.listen(3000,() => console.log('Server running on port 3000'));
The version of node is 4.2.4
When you write:-
http.createServer(function(req,res){
res.writeHead(200);
res.end("Hello world");
});
http.listen(3000);
http.createServer() returns an object called Server. That Server object has listen method available. And you are trying to access that listen method from the http itself. That's why it shows that error.
so you can write it like:-
var server=http.createServer(function(req,res){
res.writeHead(200);
res.end("Hello world");
});
server.listen(3000,function(){
console.log('Server running on port 3000')
});
Now it will let your server listen on the port 3000.
listen isn't a function of http, but a method of the server you create with createServer:
var server = http.createServer((req, res) => {
res.writeHead(200, {'Content-type':'text/html'});
res.end('<h1>Hello NodeJS</h1>');
});
server.listen(3000,() => console.log('Server running on port 3000'));