Unhandled exception in express - javascript

im getting the following error issue throw er; // Unhandled 'error' event
Here is my Controler.js
const router = require('express').Router();
router.post('/api/sendMail', async (req,res) => {
console.log("yes")
});
module.exports = router;
Here is my index.js
const cont = require('./controllers/Controler');
app.use(cont);
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::5000
at Server.setupListenHandle [as _listen2] (net.js:1286:14)
at listenInCluster (net.js:1334:12)
at Server.listen (net.js:1421:7)
at Function.listen (/Users/user/folder/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/Users/user/folder/server.js:24:5)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1313:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
at startup (internal/bootstrap/node.js:285:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
Im not sure what im missing but im gettng unhandled error isssue.

It seems like something already uses 5000 port.
If you use *nix based OS, you can get list of processes like this:
netstat -tulpn | grep 5000
and then, kill particular process:
kill -9 PID

Use try/catch:
try {
app.use(cont);
} catch(er) {
console.log(er);
}
Also note that port 5000 is already in use (EADDRINUSE) - try changing your port to something else.

Try running your app using a different port or kill the process in the current port 5000
To kill a task in windows
//get process running in port 5000
netstat -ano | findstr :5000
//enter your pid and force kill
//replace <pID> with your process id
taskkill /PID <pID> /F

Related

I am trying to build a redis nodeJS cache system

Each time i try to run my code for my redis-nodeJS system, I am given this error for one of the functions.
> events.js: 174
> throw er; // Unhandled 'error' event
> ^
>
> Error: listen EADDRINUSE: address already in use :::5000
> at Server.setupListenHandle [as _listen2] (net .js : 1280:14)
> at listenInCluster (net .js :1328:12)
> at Server.listen (net. js : 1415 :7)
> at Function.listen (/home/ richu biju /node_modules/express/lib/application . js :618:24)
> at Object.<anonymous> (/home/ richubiju /Downloads/ index8. js: 57: 5)
> at Module._compile (internal/modules/cjs /loader . js :778:30)
> at Object.Module._extensions..js (internal/modules/ cjs / loader.js:789:10)
> at Module.load (internal/modules/cjs/loader.js:653:32)
> at try Module Load (internal/modules/cjs /loader . js :593:12)
> at Function.Module._load (internal/modules/cjs/loader.js:585:3) Emitted 'error' event at:
> at emit ErrorNT (net .js :1307:8)
> at process._tick Callback (internal/process/next_tick.js:63:19)
> at Function.Module. run Main (internal/modules/cjs/loader.js:834:11)
> at startup (internal/bootstrap/node.js:283:19)
> at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
This means you or another process on your computer are already using port 5000. I'm not sure which one it is but you can try killing the port. Or a better alternative is to use another port to listen on.

process.env.PORT from dotenv causes EACESS: permission denied error in node

So I just installed dotenv in my project and I am requiring it with this code:
require("dotenv").config();
And then using my variables like so:
app.listen(process.env.PORT, () => {
console.log(`Listening on ${process.env.ENDPOINT}:${process.env.PORT}`);
});
In .env:
ENDPOINT = "127.0.0.1";
PORT = 5000;
Code works just fine if I add the variables as constants within my js file, but when I try to access the variables through process.env I get:
events.js:291
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied 5000;
at Server.setupListenHandle [as _listen2] (net.js:1299:21)
at listenInCluster (net.js:1364:12)
at Server.listen (net.js:1461:5)
at Function.listen (C:\Users\Darkbound\Desktop\TouchScreenProject\SmartFactory\server\node_modules\express\lib\application.js:618:24)
at Object. (C:\Users\Darkbound\Desktop\TouchScreenProject\SmartFactory\server\main.js:34:5)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1343:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) { code: 'EACCES',
errno: -4092, syscall: 'listen', address: '5000;', port: -1 }
I have tried using other ports, I get the same error on all ports, I have tried 3000, 3010, 5000 etc.
If the other variable, ENDPOINT is accesses without problems, so if I do:
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Listening on ${process.env.ENDPOINT}:${PORT}`);
});
This works.
EDIT SOLVED: I found the issue, as usual, right after I ask the question. The issue is that I have ; at the end of each variable in the .env file. this happened because I copy/pasted the variable from my javascript and I only deleted the const but forgot about ;
I found the issue, as usual, right after I ask the question. The issue is that I have ; at the end of each variable in the .env file. this happened because I copy/pasted the variable from my javascript and I only deleted the const but forgot about ;

Testrpc Error: listen EADDRINUSE :::8545 on Mac

I am getting an error when running testrpc
What may be the problem and how may I solve this?
EthereumJS TestRPC v6.0.3 (ganache-core: 2.0.2)
/usr/local/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:74143
var Module;if(!Module)Module=(typeof Module!=="undefined"?Module:null)||{};var moduleOverrides={};for(var key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}var ENVIRONMENT_IS_WEB=false;var ENVIRONMENT_IS_WORKER=false;var ENVIRONMENT_IS_NODE=false;var ENVIRONMENT_IS_SHELL=false;if(Module["ENVIRONMENT"]){if(Module["ENVIRONMENT"]==="WEB"){ENVIRONMENT_IS_WEB=true}else if(Module["ENVIRONMENT"]==="WORKER"){ENVIRONMENT_IS_WORKER=true}else if(Module["ENVIRONMENT"]==="NODE"){ENVIRONMENT_IS_NODE=true}else if(Module["ENVIRONMENT"]==="SHELL"){ENVIRONMENT_IS_SHELL=true}else{throw new Error("The provided Module['ENVIRONMENT'] value is not valid. It must be one of: WEB|WORKER|NODE|SHELL.")}}else{ENVIRONMENT_IS_WEB=typeof window==="object";ENVIRONMENT_IS_WORKER=typeof importScripts==="function";ENVIRONMENT_IS_NODE=typeof process==="object"&&"function"==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;ENVIRONMENT_IS_
Error: listen EADDRINUSE :::8545
at Object._errnoException (util.js:1024:11)
at _exceptionWithHostPort (util.js:1046:20)
at Server.setupListenHandle [as _listen2] (net.js:1351:14)
at listenInCluster (net.js:1392:12)
at Server.listen (net.js:1476:7)
at Server.server.listen (/usr/local/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:106030:17)
at Object.<anonymous> (/usr/local/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:50244:8)
at __webpack_require__ (/usr/local/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:21:30)
at /usr/local/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:64:18
at Object.<anonymous> (/usr/local/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:67:10)
Another process is already using the port 8545. Simply find the PID using the corresponding port and kill it:
lsof -i:8545
kill -9 PID

Node js unhandled event

I am new to nodejs. Started learning with this simple program .
var http=require("http");
var host="127.0.0.1";
var port=10016;
var server=http.createServer(function(request, responce) {
console.log("Request recieved : " + request.url);
responce.writeHead(200, {"Content-type":"text/plain"});
responce.write("Hello World.! This is the start of my journey in nodejs");
responce.end();
});
server.listen(host, port, function() {
console.log("Listening "+ host +":" + port)});
When I run the program using "node server.js " I encounter this error which , although I surfed, I could not find any relevant explanations apart from using different ports. I tried as many as any . I am a windows user. Mac users might have sudo with root permissions, but the problem isnt the same. Please help. Thanks in advance.
My error is :
C:\node>node firstserver.js
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:1132:5)
at Object.<anonymous> (C:\node\firstserver.js:12: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)
Help appreciated.!
Just swap the server.listen arguments in your code - it should be written like this:
server.listen(port, host, function() {
console.log("Listening "+ host +":" + port)});
port is the first argument, because the second one is optional. Quoting the docs:
server.listen(port, [hostname], [backlog], [callback])
Begin accepting connections on the specified port and hostname. If the
hostname is omitted, the server will accept connections directed to
any IPv4 address (INADDR_ANY).

Node.js Cannot find module 'tcp'

node is crashing at the following line:
var tcp = require('tcp'),
error text:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'tcp'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (C:\Program Files\nodejs\websocket\websocket.js:11:11)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
What is the problem? I found the source on the Internet, and the author, and the visitors also can run it...
Try require('net') instead:
$ node
> var tcp = require('tcp');
The 'tcp' module is now called 'net'. Otherwise it should have a similar interface.
> var tcp = require('net');
> $
Others are able to run, may be because they are using Node module when 'tcp' was there...
Now its called 'net', but its all the same thing no need of checking..
If you want to cross check for your more information, here are the links:
1.http://nodejs.org/api/net.html
2.https://github.com/joyent/node/blob/master/lib/net.js

Categories