nodejs web server not responding - javascript

i am trying to setup a web server using nodejs. The following code below is directly from the nodejs.org website just configured with my server credentials.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(5000, '10.0.1.51');
console.log('Server running at http://10.0.1.51:5000/');
but when i go to 10.0.1.51:5000 nothing is found i don't even get an error in my console.
also the book i am learning from provided me with this
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
and that still doesn't work. I'm not sure on where to look to resolve this issue, thanks.

Related

I'm having trouble executing a simple JavaScript code on the server using node JS

I'm a beginner with Node.js.
I have just installed node JS, and I'm trying to create and execute code through a local server (for practice purposes). But it can't be possible to execute the code using localhost:8080 on a web browser nor through the cmd (I'm on windows 7).
Below you can see what I've been trying so far...
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World! i did it');
}).listen(8080);
The browser just says that the site can't be reached and the cmd shows nothing after executing the command. So what would be the problem.
It would be a good approach to get the PORT environment variable and to have the server listen on that port, instead of one that is hard-coded.
In node you can get this with: var port = process.env.PORT || 8080
This is saying that if the PORT environment variable is set use that OR use 8080 if it is not set.
Your new code would be:
var http = require('http');
var port = process.env.PORT || 8080;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World! i did it');
}).listen(port);
If you are using Windows CMD, first type 'node'. Now you will be coding directly in node.js. Now, copy and paste your code, and hit enter. Now go, and try the localhost:8080 on your browser.
If you still have the issue, try the same with a different port.
I tested your code on my machine and works perfectly fine.

Get Node js Server Url

I am new to node.js. I just installed node.js on my production server it was installed correctly and node.js is running on my putty cli the problem is I cannot access it on my browser. I think my problem is because I installed created my server on a subdirectory inside a subdomain e.g. subdomain.example.com here is my code
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(3000); //the server object listens on port 8080
Now I am trying to access the node.js server by going to this address subdomain.example.com:3000 but I am getting no results. Please help Thanks!

Terminal stuck at white starting nodejs

I'm trying to setup a nodejs based server, but unfortunately, when ever I run it via terminal, node app.js it returns console log thing what ever I declare in app.js file, and after that, it stuck, I can't not use it to process more tasks.
var http = require('http');
var express = require("express");
var app = express();
app.get('/files/:hash/:title', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(req.params));
console.log('Done');
});
app.listen(8080);
console.log('Server running');
and terminal's response
root#mrboota13:/var/Node# node app.js
Server running
Done
Done
Done
Done
Done
Done
Done
That's because the Node process is still running, waiting for new HTTP requests to arrive.
To stop the process, press Ctrl+C

Node JS not listening to port 1337 on server

I'm trying to open a port on particular lamp server hosted by Google and I'm in connection with the server via ssh.
I've followed this link to configure nvm and the latest Node JS(v0.12.5) on it. After installing, I've used this demo code in "server.js" file and using the command "node server.js", it looks like Node JS is running, giving this message "Server ready" at the server console. Now the problem is that when I check for the open port using "netstat -n", I dont see any 1337 port open, which it should be. I've also tried to connect through browser using "serverIPaddress:1337", but I get "Conecting..." message and then nothing happens.
Any idea where I'm messing up??
I'm also confused with the server IP address(localhost:127.0.0.1) or (globalIPaddress) to put in the server.js file.
P.S: Please find the server.js file script below.
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 ready');
Try removing '127.0.0.1' or change it to 0.0.0.0 - to listen on all interfaces.
See documentation for details
https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
With current settings the server accepts connection only from localhost.
Also you need to tune firewall to open the 1337 port on remote server
You have two problems:
1) "127,0,0,1" means "localhost" - it is NOT appropriate if you want remote clients to connect.
2) port 1337 may (or may not) be open through a firewall. It sounds like it isn't.
SUGGESTED CHANGE:
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Hello World\n');
}).listen(80);
console.log('Server ready');
This is assuming that your remote server doesn't have some OTHER web server already bound to port 80. If your revised program dies with a "port in use" error, then try port 8080. Or port 8888.
Just put port number to listen on rather than using both port number and ip-adress
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 ready');
if you want to use ip adress than the server will be accessible only from local with url 127.0.0.1:1337 you will not be able to access it with localhost:1337
Yes, I've tried several times to send to the server, but sometimes it doesn't work - If you use :
"lsof -P -iTCP | grep LISTEN"
you could check the process in -bash.
And then determinate how many ports are working. You can kill the process using :
"kill -9 process_id".
"port in use" means that port is being used by other applications or program is running on it.
Like example:
iTunes some times don't let you send in the same port.
For that is better Quit the app and try to send it later.

How do I run node.js code after I installed it with xampp?

I run xampp and I was recently told by a friend to try node.js as a replacement for PHP so I thought I should give it a try
I went to node.js's website and installed the .msi file, later I put the following code in a .js 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/');
I launched xampp and executed "node app.js" from the command prompt, tried visiting localhost:1337 from my browser but it cannot resolve
What am I doing wrong?

Categories