HTTP Server on Android via node.js errors upon request - javascript

I'm running node.js v0.11 on Android (via https://github.com/paddybyers/node). I attempted to try out the "Hello HTTP" example found here: http://howtonode.org/hello-node, however, I ran into problems.
The server starts fine, but as soon as I attempt to connect to the http server (by visiting http://localhost:8000/), I get this error:
net.js:1156
COUNTER_NET_SERVER_CONNECTION(socket);
^
ReferenceError: COUNTER_NET_SERVER_CONNECTION is not defined
at TCP.onconnection (net.js:1156:3)
My code is exactly the same as the Hello HTTP example:
//Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");
How can I fix this?
Thanks!

Compile Node.js with the HAVE_PERFCTR option. (You Need to implement your own perfctrs for Android. See node_counters.cc line 130).
It should be also possible to define the missing functions in your script as empty functions.
PS.:
You also need DTRACE.

I simply commented out all COUNTER_NET_, COUNTER_HTTP_, DTRACE_NET_, and DTRACE_HTTP_ calls in the javascript files under lib/. This amounted to about 10 or so lines that I commented out of net.js and http.js.
I think that js2c.py is supposed to process src/macros.py and src/perfctr_macros.py to effectively do this 'commenting out' for you when it serializes the scripts under lib/ to C arrays in out/release/src/node_natives.h, but that doesn't seem to be happening.

Related

Failed to connect to localhost port 8000: Connection refused in Node.js

I'm trying to connect to the HTTP server in node.js.
I've spent my whole day searching online to solve my issue but unfortunately, it is of no use.
This is a simple code I wrote taking reference from official Node.js documentation.
Index.js
const http = require('http');
const requestListener = function (req, res) {
res.writeHead(200);
res.end('Hello, World!');
}
const server = http.createServer(requestListener);
server.listen(8080);
The code I am running inside CMD terminal
curl localhost:8080
This returns the following error:
I know this questions have been asked a thousands time in stack overflow but none fixed my issue. Anyone can help me?
In you code you are listening to port 8080 but from commandline you are trying 8000. Make sure you are using correct port and ensure its not in use.
You are trying to execute C:\Users\prabi\app\server.js when the real file is C:\Users\prabi\app\index.js so verify the start script in package.json and fix it.
Fire up another terminal and run the command:
node index.js
Then go back to your previous terminal window/tab and run curl localhost:8080 again. Should work.
The problem is that your code has been written well, but you aren't executing it at all. So if you don't run your node code, your http server cannot listen at the 8080 port in the first place.

Express.js piping request to php built-in server on localhost results in ECONNREFUSED

I am building a simple website using npm for development, and it is hosted with a provider with php support.
The only functionality that uses php is contact form to send email. the rest is simple html and javascript.
I use a simple php server in development started with php -S localhost:8000 to test a simple php email script and again in dev I reverse proxy requests for email.php to this php server locally.
Node app is on port 3000 and php server is on port 8000. The problem is I get connection refused error with the following express server configuration when request goes through localhost:3000/email.php:
#!/usr/bin/env node
var express = require('express');
var app = express(),
request= require('request'),
port = +(process.env.PORT || 3000);
app.set('case sensitive routing', false);
app.post( '/email.php', function( req, res ){
req.pipe( request({
url: 'http://localhost:8000/email.php',
qs: req.query,
method: req.method
}, function(error){
if (error.code === 'ECONNREFUSED'){
console.error('Refused connection');
} else {
throw error;
}
})).pipe( res );
});
// other request handlers here
app.listen(port, function() {
console.log('listening');
});
Php server is definitely up and serving all the pages on port 8000, which I can browse with the browser. I test it with curl and it seems to be handling the request just fine when posted directly to localhost:8000 using curl.
Not sure why I get this error, scratching my head, can't think of any reason.
Help is much appreciated.
I figured out what it was, d'oh! Well I am gonna post the answer in case someone else stumbles upon this.
PHP is to blame it seems; Checking the sockets listening a port using ss -ltn ( I am on Linux, this might not work for you) I realised php server is listening IPv6 only. Relevant output as follows:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 ::1:8000
With the relevant search I found the answer on web server documentation page under user notes posted by a user. See the post here. The solution is to use 127.0.0.1 rather than localhost:
As it turned out, if you started the php server with "php -S
localhost:80" the server will be started with ipv6 support only!
To access it via ipv4, you need to change the start up command like
so: "php -S 127.0.0.1:80" which starts server in ipv4 mode only.

Why is NodeJS only showing files on my browser instead of the results of my code?

I'm new into node JS. I'm trying to install node and run just a 'Hello World' app but I have problems with my server.
When I try to run my app, the server is showing only the index files and not 'Hello World'. enter image description here
The node server is working and says that the http-server is available.
My code for my Hello World app:
var http = require("http");
http.createServer(function(req, res){
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8080);
console.log('Server running at http://192.168.178.14:8080/');
I want to run the file 'index.js' (second file in first screenshot). How can I fix this?
As Mikael Lennholm and robertklep have mentioned in the comments, the issue is that you are trying to run a second server on the same address (:8080).
I tested this myself to make sure, and here's the results I got:
Running an instance of the ecstatic server on :8080, producing a very similar image to yours (using default code provided by ecstatic's documentation).
Attempting to run a regular server on the :8080 port gives me an error, EADDRINUSE :::8080 (which is expected, as the other application is already using that address). You should probably get this error when trying to run your application as well.
Now, either you or someone else is to blame. But the fact is that there is already a server running on port 8080. I'd reccomend you try to find out who set it up (if it wasn't yourself) or maybe just try using another port. For example, 8088:

Node.js Unable to load the webpage The connection has timed out

I'm a beginner to node.js, I installed it as here:
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
I tried it from console and it worked:
console.log('Hello World!');
nodejs helloConsole.js
> Hello World!
Then I tried to make it within HTTP Server, here is the code:
var http = require("http");
http.createServer(function (request, response) {
   request.on("end", function () {
      response.writeHead(200, {
         'Content-Type': 'text/plain'
      });
      response.end('Hello HTTP!');
   });
}).listen(8080);
I run it from terminal:
nodejs hello.js
then I open the browser at http://localhost:8080/ , it takes a long time to load, then at chrome it gives me that:
Unable to load the webpage because the server sent no data.
and firefox gives me that:
The connection has timed out
Note : other web servers works fine, like apache.
You are listening to the "end" event of the request parameter, but at the time your outermost callback function is called, the request has already ended, so it is too late to subscribe to that event.
You can directly respond from the outermost callback, which is what the sample code in nodejs.org shows.
You attached a listener to the request object, listening for the end event, you will not see a response until that event has been caught by request.
To test things out you might want to modify it like the one provided as example here: http://nodejs.org/
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Try the following steps, if you are using windows, it may help you.
Download the node.exe from http://nodejs.org/download/
Place in a Hard Disk Drive from where you want to run like D:/nodejs/node.exe
Now place your hello.js in the same directory it should look like D:/nodejs/hello.js
Now from Command Prompt Go to that folder D:/nodejs/ and run command node hello.js

How I can run this script in node.js?

I have install all modules need to run this script using CMD. When I am run this script in node.js through webmatrix:
var http = require('http');
var everyauth = require('everyauth');
var app = require('express').createServer();
app.get('/', function(request, response) {
response.send('Hello Express!!');
});
app.listen(2455);
It's not working. The error is:
iisnode encountered an error when processing the request.
HRESULT: 0x2
HTTP status: 500
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 node.exe process has not written any information to the stdout or stderr.
I have tried with different different script from blog but they do not work. I tried Steven Sanderson's template in webmatrix and that does work.
To use your application with iisnode, you have to use port/socket supplied by iisnode (as in this case you're using IIS as a webserver instead of the one bundled in node).
Replace the last line with app.listen(process.env.port || 2455);, so that it will both work well with iisnode and be available on port 2455 when run with node.exe app.js.
Use
app.listen(process.env.port || 2455);
instead of `
app.listen(portno);
cause it's iisnode web server.

Categories

Resources