nextTick error in node.js - javascript

I am having trouble understanding the error,
Server running at 204.232.212.213 on port 8124
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: listen EADDRNOTAVAIL
at errnoException (net.js:646:11)
at Array.0 (net.js:732:28)
at EventEmitter._tickCallback (node.js:192:41)
the code is as follows:
var http = require('http');
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8124, "204.232.212.213");
console.log('Server running at 204.232.212.213 on port 8124')
Please help me with the following, new to javascript and node.js

solved by using local address 0.0.0.0 and port 8000

Related

node.js get https no responding 504

I am trying to make an https request in my backend node.js web app. I have the following code:
const express = require('express');
const https = require('https');
const app = express();
app.get("/", function(req, res) {
const url = "https://www.tkmaxx.com/uk/en/women/edits/big-brand-drop/c/01240000/autoLoad?page=1"
https.get(url, function(response) {
console.log(response.statusCode);
});
res.send("running test")
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
I am getting the following error:
node:events:504
throw er; // Unhandled 'error' event
^
Error: socket hang up
at connResetException (node:internal/errors:691:14)
at TLSSocket.socketOnEnd (node:_http_client:466:23)
at TLSSocket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketOnEnd (node:_http_client:466:9)
at TLSSocket.emit (node:events:538:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) { code: 'ECONNRESET' }
Anyone know what's going on? Is the issues related to request headers or user agent? How can I set that?
The request is only accepted by the remote server if it has an Accept header and also Connection: keep-alive. (These are headers a browser typically sets.)
https.get("https://www.tkmaxx.com/uk/en/women/edits/big-brand-drop/c/01240000/autoLoad?page=1", {
headers: {
accept: "text/html",
connection: "keep-alive"
}
}, function(response) {...});
(Perhaps this is a mechanism which the remote server employs to guard against requests made by clients other than browsers?)

node-transmission package not working

I tried installing this package : https://github.com/FLYBYME/node-transmission in my local nodejs installation but I am getting following error while running example.js from the above github repository.
Error: connect ECONNREFUSED 127.0.0.1:9091
at Object.exports._errnoException (util.js:1022:11)
at exports._exceptionWithHostPort (util.js:1045:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 9091
After a bit research I tried to run a server at port 9091 using this code(in a separate server.js file)
const http = require('http')
const port = 9091
const requestHandler = (request, response) => {
console.log(request.url)
response.end('Hello Node.js Server!')
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
After running a server on port 9091, I started getting this error with example.js :
SyntaxError: Unexpected token H in JSON at position 0
at JSON.parse (<anonymous>)
at IncomingMessage.onEnd (F:NodeJS\node-transmission-master\lib\transmission.js:453:33)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
What must be causing this? I have no idea which step I have done wrong. That's why I described the whole process I followed.
I am very new to nodejs. Any help will be deeply appreciated.
The library is expecting a JSON formatted response and you are sending a simple text response. If you look through their source code you can see that their callServer function expects a stringified JSON but I can't see that in their docs.
You can change your code like so:
const http = require('http')
const port = 9091
const requestHandler = (request, response) => {
console.log(request.url)
// Format your response as a stringified JSON
response.end(JSON.stringify({message: 'Hello Node.js Server!'}));
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
In order to run the example from the node-transmission package, you need first to install and start the transmission-daemon.
The following steps are for Ubuntu:
Login as root or change to root with su - (be always careful what you do/install as root)
Install the transmission-daemon linux package: apt-get install transmission-daemon
Edit the daemon configuration for either disabling authentication or setting up your username/password (there is no default password). You can disable authentication by editing the relative flag in the configuration file:
pico /etc/transmission-daemon/settings.json
Set the auth flag to false: rpc-authentication-required:false
Press Ctrl-X then Y and then Enter to save the change
Start the daemon: start transmission-daemon
You should be able now to execute successfully the example.js and download its torrent link.

Error EADDRNOTAVAIL with Heroku nodejs server

I have developped a nodejs server on OpenShift and I am now trying to make the same kind of server on Heroku for a new project.
Here is a minimal code of my server:
var http = require('http');
var port = process.env.PORT || 8080;
var address = process.env.IP || '127.0.0.1';
console.log(address);
console.log(port);
var server = http.createServer(function(req, res)
{
res.writeHead(200, { 'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*' });
res.write(JSON.stringify({ valid: true }));
res.end();
});
server.listen(port, address);
Differences with my OpenShift server are:
The replacement of variables process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP by process.env.PORT and process.env.IP.
I set the IP variable with this command line: heroku config:set IP=MYSERVERADDRESS.com
I cannot start my server, it always crash and I don't understand why, the logs of my server are:
MYSERVERADDRESS.com
41184
events.js:154
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL MYSERVERIP:41184
at Object.exports._errnoException (util.js:893:11)
at exports._exceptionWithHostPort (util.js:916:20)
at Server.__dirname.Server.Server._listen2 (net.js:1233:19)
at net.js:1391:9
at GetAddrInfoReqWrap.asyncCallback [as callback] (dns.js:63:16)
at listen (net.js:1282:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:82:10)
My guess would be that your Heroku server will not have your (external) server address as its (internal) IP-number (the external IP-address will most likely terminate earlier in the Heroku network stack), which means that you cannot explicitly listen on it (which is basically what EADDRNOTAVAIL means).
Instead, don't use an address to listen to at all:
server.listen(port);

Node.js custom Module

When I run app.js, I get this error:
MBPdiDaniele3:Desktop danielemartini$ node app.js
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:8080
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
MBPdiDaniele3:Desktop danielemartini$
Here's the code for make_request.js:
var http = require('http');
var makeRequest = function(message) {
var options = {
host: 'localhost', port: 8080, path:'/', method: 'POST'
}
var request = http.request(options, function(response) {
response.on('data', function(data) {
console.log(data);
});
});
request.write(message);
request.end();
};
module.exports = makeRequest;
Here's the code for app.js:
var makeRequest = require('./make_request');
makeRequest("Here's looking at you, kid");
makeRequest("Hello, this is dog");
There are several possible causes :
No service is running on localhost:8080
A service runs on 8080 which refuses connections actively.
To check what is running, use one of those 2 commands (Unix) :
lsof -i :8080 -S
netstat -a | grep 8080
3 - Your running service isn't bound to your internal IP.
I've encountered the issue a few times on Cloud servers, where localhost/127.0.0.1 are not recognized. Try using the external IP of your machine (and make sure the firewall lets you make requests), or force your service to bind to all interfaces.
Hope it helps.

Node.js - simple example and EPERM error

var server = require('http').createServer(function(req, res){
});
server.listen(8080);
This simple example return error in console:
node node_server.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: listen EPERM
at errnoException (net.js:670:11)
at Array.0 (net.js:756:28)
at EventEmitter._tickCallback (node.js:192:41)
What is going on?
Port was locked. Topic shutdown.

Categories