Node JS - Server doesn't react to requests - javascript

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.

Related

Nodejs webserver use script port 3000

I created a script in nodejs that extract data from database and create a file with all db data. I create also a webserver node js listening on port 3000 and with forever is working in listening mode all time. But now the script is without web interface and use a command line prompt and other modules. How remote users can use a remote script like : node scrip.js like a command shell prompt on locale machine ?
This code working but in-globe only hello world not my prompt command and db retrieve info. Thanks
var app = connect().use(connect.static('public')).listen(3000, "0.0.0.0");
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');
I'm not completely sure I understand what you are asking. But if you are just asking how to communicate with a local server running on port 3000.
Then if this is a GET request, you could simply type 'http://localhost:3000/' in a browser, or you could use a tool like postman, I recommend the Chrome app.
Local server is listening on port 3000, but i can see only hello world. But when one user access to my url http://ip:3000/ needs to use a command shell like >
node script.js because i have different operations db retrieve information and input user data from command line input.

node server running but localhost refusing to connect

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

Unable to get Node.js hello world application working [Fedora 21]

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

How to run a website developed with node.js in local?

I would like to run a website developed with node.js in local.
I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js )
Also, I guess I have to simulate a server ? How can I do that with node?
You can start a simple server with the example that can be found on 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/');
https://nodejs.org/en/about/
To develop a website it is very helpful to use a web framework such as Express.
http://expressjs.com/
You should use:
npm start file.js
but also be sure to check out nodemon, which is very helpful for debugging - it restarts your app on code change.
Also be sure to check out the express generator, which will set up a node+express app that you can check out to figure how to get the server and routes going.

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

Categories

Resources