Nodejs hello world - node.js

Hi guys I was hoping for a little help , My overall goal is to make an web scraper running from an server. But I figured I would have to learn how to walk before trying to run ...
So I have simplified my problem to this:
How to get response in browser window for simple hello world app?
This is what I have done
Created an server at digital ocean.
setup the server.
Setup Putty and fileZilla
So far all of this work. But how can I get this little code to work:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, '178.62.253.206');
console.log('Server running at http://178.62.253.206:3000/');
When I run this from the PuTTY terminal I get this response in the console window:
- Server running at http://178.62.253.206:3000/
But when I navigate to this in chrome browser I get this error:
Failed to connect to 178.62.253.206 port 3000: Connection refused
What Im I doing wrong ?
Any help at all would be much appreciated
Frederik

You're only accepting connections from 178.62.253.206. Change this:
}).listen(3000, '178.62.253.206');
To this:
}).listen(3000);

Related

I can't run my node application in my on prem VM

I'm trying to run a node js application on an on-prem VM which is running RHEL 7. I'm not experienced in RHEL 7 and can't seem to find any details in running Node JS apps on it.
My app is super simple. Is a server which returns a message...
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome Node.js');
}).listen(8000);
console.log('Server running on http://ip:8000');
I run the application and try accessing the IP with the port in the browser within the correct network. Am I missing something? My assumption was RHEL7 was the server and would show me the application once I open it on the browser.

node.js - Displayed page in browser does not change after pushing new code

I have an 'hello world' Node test app running fine on ubuntu remote server ( Nginx proxy, PM2 managed )
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/');
running fine ( locally on ubuntu server :
curl http://127.0.0.1:8080
Hello World
and via web browser
When I update the code :
changing to: res.end('Hello World, again');
and pushing it via scp :
scp -v -i ~/.ssh/id_rsa.pub -r hello.js myself#myRemoteServer:/home/myself
hello.js is effectively modified on the remote server , BUT the running Node app hello.js is still displaying the previous text ...
what am I missing to make the new code running ?
thanks for feedback
You need to restart the app in order to make it use the new code.
pm2 restart hello // or however your app's name is
should do the trick after uploading the new files.
There's also an option to auto restart your app on file change. For this, start your app with
pm2 start hello.js --watch

connecting to node.js http server on linux machine from windows machine

I'm trying to write my first node http server. I have got it running on my linux box. If I type the url in the browser. I see the hello world webpage.
myLinuxHostName:1227/
I'm now trying to connect to this linux node server from my windows machine. If I type my in the browser from my windows machine the same url I get webpage not available. I tried pinging my linux host and that worked fine. What am I doing wrong?
I'm using the simple http server code that is there on nodejs.org homepage.
If you are using this example:
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/');
Then only runs using this exactly ip 127.0.0.1 whats mean localhost and the other VHost not reach that server. You must doing something like this.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337);
For more information: http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback

Can't access node.js script from web browser

I installed node.js and socket.io in my CentOS 6.4 server. It is running parallel with Apache, with Apache as main server (port 80)
I wrote a simple Hello world script to test node.js installation:
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello World\n');
response.end();
}).listen(8088);
console.log('Server started');
If I run it in command line I get 'Server started'. But when I tryh to access it via web browser, typing http://xxx.xxx.xxx.xxx:8088 it never loads. I've tried to use many other port numbers with no success. I have to ips in my server, but neither of them work, nor my domain addres under those ips.
How could I fix it?
EDIT: node,js is installed in another server, and I'm trying to access it via webbrowser from outside this server.
Thanks!
i think you need to open port 8088 by firewall on server.
see man iptables

Node.js on MAC: Access a Node.js web server from another computer

I built a Node.js web server on my computer, using the so-well-known-http-web-server-example of Node.js:
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
res.end('It works');
}).listen(3000, '127.0.0.1');
This works (as expected) on the computer that runs the server.
I would like to access the server from another computer, in the same LAN. Using ifconfig on the terminal of the computer that runs the server (Apple MacOSX), I get: 192.168.0.6.
So, in my other computer, I opened my browser and connected to http://192.168.0.6:3000, but I get:
Oops! Google Chrome could not connect to 192.168.0.6:3000
My final aim, is to be able to connect to the server using my smartphone.
Any help would be welcome. Don't hesitate to ask for more details if necessary.
Thanks in advance :)
127.0.0.1 is only local interface. Try to start listening all interfaces:
var http = require('http');
http.createServer(function(req, res){
res.writeHead(200, {'content-type': 'text/plain'});
res.end('It works');
}).listen(3000, '0.0.0.0');

Resources