So I download Windows binaries from node's site and installed on my Windows 7 machine which is installed fine, when I do:
node --version
It correctly displays its version: v0.6.7
Here is the hello world program:
// app.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8000/');
And when I do:
node app.js
I get the response fine:
Server running at http://127.0.0.1:8000/
However, when I browser the URL http://127.0.0.1:8000, the page keeps on running (on status bar it says waiting for 127.0.0.1...).
Can anybody help me how to make it to output Hello World ?
This is what I get with curl:
$ curl -vv localhost:8000
* About to connect() to localhost port 8000 (#0)
* Trying ::1... Connection refused
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:8000
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Connection: keep-alive
< Transfer-Encoding: chunked
<
Hello World
* Connection #0 to host localhost left intact
* Closing connection #0
nothing wrong with it. Try updating node to the latest stable version (but I don't see how it can help) and be sure any process except node is owning the tcp port 8000.
In my mind, this is either one of two things:
Node does not recieve the request
Node does not respond to the request
To test for either, change your code:
// app.js
var http = require('http');
http.createServer(function (req, res) {
console.log('got a request');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8000/');
Try again.
If node does not get the request, try changing the port to 80 instead of 8000, I had a funny case with firefox blocking non-conventional ports a couple of weeks ago.
Let me know how you go
Have you checked your Anti Virus or Windows Firewall?
Whichever one you use you might want create a temp exception or disable it for a while just to make sure nothing is getting in the way there.
Try changing your listen address to 0.0.0.0. Maybe you're having problems binding to that specific loopback.
Related
Trying to get the simplest node server interacting with the browser, with this:
var http = require('http');
http.createServer(function(req, res){
res.writeHead( 200, { "content-Type" : 'text/plain' } )
res.send('Hello world');
}).listen(1337, '192.168.1.2');
but localhost won't do it..
localhost refused to connect
Thats the IPv4 address. Am I missing something here?
Use 0.0.0.0 and it will work both '192.168.1.2' and localhost.
I suspect you are on a Mac. First change 192.168.1.2 to 127.0.0.1, or 0.0.0.0 if you want to be accessible from other computers.
Next browse to http://127.0.0.1:1337/, not http://localhost:1337/. For whatever reason localhost resolves to ::1 - an IPv6 address on my Mac. For some reason Node immediately disconnects any IPv6 connections on my machine.
Had the same problem and solved it like this:
var http = require('http');
http.createServer(function(req, res){
res.writeHead( 200, { "content-Type" : 'text/plain' } )
res.end('Hello world');
}).listen(1337);
On node Terminal: node filename.js
Leave it open, don't CTRL + C.
Go to webpage: localhost:1337
It should work. The problem is not closing the terminal or exiting the execution of the node File.
I think you have just a typo, use res.end to send data back and close the connection instead of res.send.
Also, you shouldn't have any problem to connect to localhost
var http = require('http');
http.createServer(function(req, res){
res.writeHead( 200, { "content-Type" : 'text/plain' } )
res.end('Hello world');
}).listen(1337, 'localhost');
Use these three steps:
Type netstat -aon on the Command Prompt
Find the relative PID number to 127.0.0.1:8080 (or 3000)
Run taskkill /f /pid pidnumber
If you want to connect using localhost you have to use "localhost" at listen.
You have to remove the IP address you've added after the port number. Your localhost already has an ip address assigned.
I was having this problem due to closing the CMD.exe window.
Try changing your port to another number and try it again
I'm trying to write a node.js application, trying to make sure my environment set up first.
The webserver is Fedora 21, and I have opened port 3000 on the firewall and flushed ip tables. nmap localhost indicates that port 3000 is listening.
I'm using this node application
var http = require('http');
var PORT = process.env.PORT || 3000;
http.createServer(function (req, res) {
console.log('%d request received', process.pid);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello world!\n');
}).listen(PORT);
console.log('%d listening on %d', process.pid, PORT);
But when I navigate the browser to my server I don't get any hello world message like intended.
Perhaps check for firewall rules? IIRC many firewalls prefer to drop packets instead of actively rejecting -- so you may find that (eventually) the web browser times out.
Easy way to check:
In one console, do: nc -l -p {whatever your PORT environment variable is set to, or 3000, as per your server logic}
and in another console, do
echo hello | nc localhost 9999
If you see the hello in the first console, you can at least rule out some tcp oddness / firewall issue.
Conclusion I'm an idiot who needs another cup of coffee.
it works 100% just instead of navigating to hostname/webapp I need to navigate to hostname:3000
I am new to NodeJS and learning, I want to know how to make an sample app using NodeJS.
I have tried it on localhost and it is working. Now I am trying to host it public to any server, but it is not working.
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/');
This is code is saved in my local machine as m.js, I run:
$ node m.js
It runs fine and when i open it in browser as https://127.0.0.1:1337
I am getting the output as :
Hello World
I want the same thing to do from a remote server i.e. I have a domain www.example.com and if I open it with https://www.example.com:1337
then it should show on my browser screen like previously :
Hello World
But it is not working.
#Annanta 127.0.0.1:1337 is a local machine ip address. please remove this.
Please find below the sample code. Try to access it with remote server ipaddress. Please note the remote machine must have node and npm installed.
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(1337);
console.log('Server running');
Here is a possible work-around using an Apache2-Webserver:
Just edit the Virtual Host in your conf.d (for Ubuntu you´ll find it in /etc/apache/), run a2enmod proxy and restart the Apache.
Here is a possible configuration:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName your-domain.com
ServerAlias www.your-domain.com
ProxyRequests off
ProxyPass / http://127.0.0.1:1337/
ProxyPassReverse / http:/127.0.0.1:1337/
</VirtualHost>
Its actually really simple. Just don't use any IP at all and just define the port.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337);
console.log('Server running!');
Also as Sach stated you can't just upload it to a webserver. You have to have node and npm installed and you have to actually run the code via
$ nodejs application.js
I set up a Node JS server, and made a request to it, it just loads and loads and eventually says "Server not found". Here is the code for my file:
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/');
When going to externalIP:1337, the phenomenon described above happens. I am running Ubuntu 14.04, node JS version 0.10.32.
What is going on?
You're specifically listening to 127.0.0.1 which is localhost. If you want to allow connection via the external IP, you should omit the '127.0.0.1' argument in your listen. i.e. change listen(1337, '127.0.0.1') to listen(1337). Otherwise go to localhost:1337 instead.
The problem is that you're only listening for requests on localhost. If you try to access the server from outside the system you won't get there because the server isn't listening on a LAN IP.
Change
.listen(1337, '127.0.0.1');
to
.listen(1337);
That will listen on all available network interfaces on the system. You could specify a LAN IP (just like you did for localhost) if you wanted to listen on a specific network interface.
Sorry.
Apparently tomcat was also using port 80. So by disabling tomcat I got it to work.
Thanks.
Answered/fixed:
Oops! 10000 points to jimw!
I'm trying to start a hobby project using Node. So I got going:
installed Node 0.6.14
copy-pasted the "hello world" program from their homepage in Eclipse
saved as Main.js
launched (node Main.js)
opened http://127.0.0.1/1337
and then I get this:
Firefox can't establish a connection to the server at 127.0.0.1.
Code:
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/');
Firebug output:
Aborted - 127.0.0.1
Windows Firewall disabled - same result.
Checked netstat:
netstat -na | findstr 1337:
TCP 127.0.0.1:1337 0.0.0.0:0 LISTENING
Checked that Node is running - it was.
Changed the port - same result.
Tried to find Node server logs - can't seem to find any.
Any ideas? Right now I'm kind of baffled that "Hello world" isn't working (!)
I don't know how to close the question, so I'll just answer myself.
jimw's comment was the answer - I was trying to access the wrong port because I entered localhost/1337 instead of localhost:1337, I was just blind.
Thanks a lot ;)