Node js unhandled event - javascript

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).

Related

throwing connection Error after uploading the nodejs code on live server however working fine on localhost . I was using mssql to establish connection

I have uploaded the code on the live server .but whenever I hit the URL (http://hrm.azeemaftab.com/) I am getting following Error:-
iisnode encountered an error when processing the request.
HRESULT: 0x2
HTTP status: 500
HTTP subStatus: 1002
HTTP reason: Internal Server Error
You are receiving this HTTP 200 response because system.webServer/iisnode/#devErrorsEnabled configuration setting is 'true'.
In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem.
The last 64k of the output generated by the node.exe process to stderr is shown below:
Application has thrown an uncaught exception and is terminated:
E:\Inetpub\vhosts\azeemaftab.com\hrm.azeemaftab.com\node_modules\mssql\lib\tedious\connection-pool.js:39
cfg.options.connectTimeout = cfg.options.connectTimeout ?? this.config.connectionTimeout ?? this.config.timeout ?? 15000
^
SyntaxError: Unexpected token '?'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (E:\Inetpub\vhosts\azeemaftab.com\hrm.azeemaftab.com\node_modules\mssql\lib\tedious\index.js:4:24)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
here is my database connection code config.js
const config = {
user:"user",
password: process.env.PASSWORD,
database:"********",
server:"*****",
port : *****,
TrustServerCertificate : true,
options: {
trustedConnection: true,
encrypt: true,
enableArithAbort: true,
trustServerCertificate: true,
},
};
I don't know why I am getting this error . while It is working fine on localhost.
You should check what version of the node the live server is running. You need to be running node version 14 or higher.

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 ;

Unhandled exception in express

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

socket.io client side in node js

Am I doing something wrong? When I run node client.js. I am getting this error message:
/web/ws/node_modules/socket.io/node_modules/engine.io/lib/server.js:358
var listeners = server.listeners('request').slice(0);
^
TypeError: Object http://localhost:8080 has no method 'listeners'
at Server.attach (/web/ws/node_modules/socket.io/node_modules/engine.io/lib/server.js:358:26)
at Function.attach (/web/ws/node_modules/socket.io/node_modules/engine.io/lib/engine.io.js:124:10)
at Server.listen.Server.attach (/web/ws/node_modules/socket.io/lib/index.js:226:21)
at new Server (/web/ws/node_modules/socket.io/lib/index.js:51:17)
at Server (/web/ws/node_modules/socket.io/lib/index.js:39:41)
at Object.<anonymous> (/web/ws/client.js:2:14)
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)
Here is my code:
var io = require('socket.io');
var socket = io('ws://localhost:8080');
socket.on('connect', function () {
socket.send('hi');
socket.on('message', function (msg) {
// my msg
});
});
Please suggest something. where I am doing the mistake ?
The problem here is that you are trying to use socket.io package which is for configuring the server.
Instead you need to use socket.io-client package to be able to act as a client.
You can find usage examples by following provided links.

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