node.js server not running - javascript

I am trying to learn node.js, but I'm having trouble getting the simple server to run on localhost:8888.
Here is the code for server.js:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
server.js runs without errors, and trying netstat -an | grep 8888 from terminal returns
tcp4 0 0 *.8888 *.* LISTEN
However, when I go to localhost:8888 in a browser, it says that it cannot be found.
I've looked at all the related questions, and nothing has worked so far. I've tried different ports, etc. I know that my router blocks incoming traffic on port 8888, but shouldn't that not matter if I'm trying to access it locally? I've run tomcat servers on this port before, for example. Thanks so much for your help!
node.js version: v0.6.15
OS: Mac OS 10.6.8

This code works--I just tried it and navigated to localhost:8888 and saw the expected output.
It's possible that you have a firewall that's too restrictive or you have a bad install of Node.js.

Related

Node.js localhost:3000 refuses to connect

I am a total beginner of Node.js and I am unable to connect to localhost:3000
I use the following code in VS code, hit "node app.js" in terminal, and there is no error comes out in terminal at this point.
However, as I try to access the localhost:3000, it keeps refusing: "ERR_CONNECTION_REFUSED"
I searched on the internet for solutions and tried opening ports by creating an inbound rule on security settings, turned IIS on, used 127.0.0.1 instead, and still get refused. Does anyone have any idea how to solve this?
I am using Windows 10
const http = require('http');
var server = http.createServer(
(request, response)=>{
response.end('hello');
}
);
server.listen(3000);
Here is how to fix it. Your probably try to launch your server on a used port.
// enter this command in your terminal
lsof -i:3000
// this will output the related PID (process ID). Here: 1382.
node 1382 name 21u IPv6 blabla 0t0 TCP *:3000 (LISTEN)
// kill the PID in use
kill -9 1382
//relaunch your server
node app.js
I ran it on my computer and that code works fine. I would try other ports to see if they work.

How to access the Node server hosted in my laptop from a device in another network?

I have written this test.js
var http = require("http");
http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// Send the response body as "Hello World"
response.end('Hello World\n');
}).listen(8080,'192.168.1.5');
// Console will print the message
console.log('Server running at http://192.168.1.5:8080/');
I runned the command node test.js
C:\Users\ShalomAlexander\Documents>node test.js
Server running at http://192.168.1.5:8080/
This URL is accessible from my laptop and devices connected to the same wifi network. but I'm not able to access it when I switch from wifi network to mobile network on my smartphone.
so how can I make it work on other devices in different networks?
You need to either...
host your app on a public server (e.g. a cloud provider) or
make your machine publicly available
Second option requires you to do a port opening & proper forwarding from your router to your machine. Also, you probably want some dynamic DNS entry for your IP, because in general you're not having a fixed IP. Just some hints for this - I don't know what your use case is.
You need to host your NodeJs application on a server(eg: heroku). Then you can access it with the hosted URL.
Please check the documentation on deploying your NodeJs application in Heroku.
https://devcenter.heroku.com/articles/getting-started-with-nodejs

Node.js serves "index of /"

I've been experimenting with Node.js on my windows machine and also my linux machine to create a web server using node.
If I try my code on windows and activate my server with "node server.js" and navigate to localhost:8080 I am met with "Hello World!" as intended.
But if I try on my linux box with the same code it shows me the directory instead of serving the page. Not sure what's going on here!
Here's the code I've been using
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
I've tried using my local ip to the machine and also using the domain followed by ":8080", both give me the same results.
I cant find anybody else with the same problem and I've been looking for hours.
I got the same issue while manually starting server from windows machine.
using command http-server -p 4200
Say, I was trying to start the server from D:/ProjectName. When I changed that and started from D:/ProjectName/build, it was working fine. Build folder has the index file and all other files.

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 to set up Node.JS with Express and Socket.IO?

I'm trying to build a Node.JS server to listen on port 3800 of my CentOS server this way:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.send('<h1>Hello World!</h1>');
});
http.listen(3800, function(){
console.log('Listening on port: 3800');
});
I have a domain and I configured this domain on my apache server with virtual host to listen on port 80. But when I try to access
http://example.com:3800 or http://server_ip:3800
it's not working. The browsers keeps trying to connect and then I got the error.
I don't know what I did wrong, since I followed the tutorial. I searched other questions here, I tried to copy the code into my index.js and nothing. This simple "Hello World" is not showing and I can't access the server.
I did the "node index.js" and on my server is showing "Listening on port: 3800" perfectly, I have root access and I did everything with the root user. I did the "npm install express" and "npm install socket.io" commands too, and I tried to make the package.json file and then "npm install". I searched another website and I tried their instructions with "npm install --save: express", won't work too.
I think it's a problem with my Linux configuration.
My question is: how I can make this simple script work when I access http://example.com:3800?
The correct answer to my question is to open the port at the iptables using this command:
iptables -I INPUT -p tcp --dport 3800 -j ACCEPT
And then saving it with the command:
/sbin/service iptables save
CentOS6 based system.
Your actual example will work going to localhost:3800
Im not sure how is that of running nodejs in an apache server :/
if it helps, I normally do it slightly different than your code:
var app = require('express');
var http = require('http');
app.get('/', function(req, res){
res.send('<h1>Hello World!</h1>');
});
app.listen(3800, function(){
console.log('Listening on port: 3800');
});
EDIT-------------------------------
Your main issue here is that you are trying to use nodejs in Apache as I mention above, is not impossible but im sure is a pain to configure your server, and will not be the normal LAMP stack configuration, and you will probably run in some kind of bugs or cutted functionalitys, try to host your app in a node backend, something like openshift or heroku

Categories