I am trying to learn nodejs. Trying to execute an example
var server = require('net').createServer();
var port = 4001;
server.on('listening', function() {
console.log('Server is listening on port', port);
});
server.on('connection', function(socket) {
console.log('Server has a new connection');
socket.end();
server.close();
});
server.on('close', function() {
console.log('Server is now closed');
});
server.on('error', function(err) {
console.log('Error occurred:', err.message);
});
server.listen(port);
when i run this .js file - node ...js I get a message - server is listening on port 4001
now i cant type anything in console (windows) the tutorial says execute telnet localhost 4001. if i do ctrl+c and then type telnet localhost 4001 it gives err 'telnet is not recognized as an internal or external command.
Open another console and run telnet localhost 4001 there.
And to install telnet, follow these instructions: http://technet.microsoft.com/en-us/library/cc771275(v=ws.10).aspx
Related
I have a node server which when I run is not showing server is running at port 5000 for example. Also, I got 0 errors in the terminal.
My question is how to check for errors that are causing the server not to run ?
app
.listen(port, () => {
console.log(`Server is running at port: ${port}`);
})
.on('error', function (err) {
console.log(err);
});
Here is the code that I use to run to listen to requests.
I have created a tcp server and a tcp client.
In my tcp client, I have specified the localPort because I want to use some specific ports in case of reconnection.
Below is my tcp server and client's code
TCP SERVER
var net = require('net');
var server = net.createServer(function(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
socket.on('error', function(err){
console.log('on socket error', err);
})
});
server.on('error', function(err){
console.log('on error',err);
})
server.listen(1337, '127.0.0.1');
TCP CLIENT
var net = require('net');
var client = new net.Socket();
client.connect({port: 1337, host: '127.0.0.1', localPort: 10002}, function() {
console.log('Connected');
client.write('Hello, server! Love, Client.');
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
client.on('error', function(err) {
console.log('error in connection',err);
});
I have this server and client in two separate files.
If I run the server, and then run the client for the first time, everything works fine.
But as soon as I re-run(reconnect) the client, it gives me an error
{ Error: connect EADDRINUSE 127.0.0.1:1337 - Local (0.0.0.0:10002)
at internalConnect (net.js:964:16)
at defaultTriggerAsyncIdScope (internal/async_hooks.js:281:19)
at net.js:1062:9
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickCallback (internal/process/next_tick.js:181:9)
at Function.Module.runMain (module.js:696:11)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
errno: 'EADDRINUSE',
code: 'EADDRINUSE',
syscall: 'connect',
address: '127.0.0.1',
port: 1337 }
It works fine if I retry after around 30 seconds.
Does net in nodejs have a TIMEOUT of not using the same localPort for 30 seconds?
If it is so, can we reduce it or manage it somehow?
Here's the app.js, the code is too long so that's why I'm showing this code only, there's no problem in other code I assume this is a network problem.
app.js
app.listen(8080, 'localhost', function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
I don't get any response when i run lsof -i :8080. but I do get response when I run curl localhost:8080 on the server.
and I don't think there's any problem with security group. I allowed any ip to access to the instance as you can see below.
and here's actually how it looks like when I test public ip and localhost
ubuntu#:ip~/$ curl -v 18.217.107.76:8080
* Rebuilt URL to: 18.217.107.76:8080/
* Trying 18.217.107.76...
* connect to 18.217.107.76 port 8080 failed: Connection refused
* Failed to connect to 18.217.107.76 port 8080: Connection refused
* Closing connection 0
curl: (7) Failed to connect to 18.217.107.76 port 8080: Connection refused
ubuntu#ip:~/$ curl -v localhost:8080
I get response here!
I changed the code from
app.listen(8080, 'localhost', function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
to
app.listen(8080, function () {
console.log('Express started on http://localhost:' + 8080 + '; press Ctrl-C to terminate.');
});
now it's working
This is what worked for me!!
In your security group you have added the rule HTTP which listens by default on port 80.
So basically if you have configured your node server to run on a port other than port number 80 (I was doing this mistake) and try to access the public DNS(EC2 public DNS can be found in instance description) on browser, connection refused error might come so what you can do is change the PORT value in the config to 80.
Your config.env will look like this
PORT=80
And in your server.js you can write
const PORT = process.env.PORT;
try {
app.listen(PORT, () => { console.log(`server running at port ${PORT}`) })
} catch (error) {
console.log(error)
}
node project run completely fine on localhost but not able to run on domain, Please help me to resolve this this.
app.listen(3000,'APP_PRIVATE_IP_ADDRESS' function(err,rslt){
if(err){
console.log(err);
}
else{
console.log("App Started on PORT 3000");
}
})
when i run the node server.js on terminal it print the message "App Started on PORT 3000" but when i run on the web it shows the error site can't be reached.
Try using '0.0.0.0' symbolic IP, it means bind all IP's or any IP.
app.listen(3000, '0.0.0.0', function(err, rslt){
if(err){
console.log(err);
}
else{
console.log("App Started on PORT 3000");
}
});
Hostname is optional, so this is equivalent:
app.listen(3000, function(err, rslt){
if(err){
console.log(err);
}
else{
console.log("App Started on PORT 3000");
}
});
In the case of that you want to specify the IP version, this two usage make difference. If you provide '0.0.0.0' value for hostname parameter, only IPv4 binding happens and only IPv4 request are accepted and listened. If this parameter is not specified, both IPv6 and IPv4 bindings happen.
I'm using NodeJS with express to create a Websocket application with WS on EC2(Ubuntu).
I have the following code:
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8090 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
Also, I added a "custom tcp rule" to the port 8090 and anywhere(0.0.0.0/0)
After I run:
wscat --connect http://myipaddress:8090
I got:
error: Error: connect ECONNREFUSED myipaddress:8090
Am I forgetting something in Ec2 Configuration or code?
Greetings
Looks like you are trying to connect using the HTTP protocol. Try running wscat --connect ws://myipaddress:8090.