cant run test node.js http script : connection not found - linux

In Kubuntu I have installed node.js sudo apt-get install nodejs
Then I made a js file called example.js containing
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 I ran the file with node example.js
but I do not see the log
In the browser I get a error saying connection not found.
if I ping http://127.0.0.1 then I get a error saying unknown host
what am I doing wrong? have I installed node.js incorrectly?

Since Ubuntu and its flavors already have a node, Node.js goes by another name on these systems:
There is a naming conflict with the node package (Amateur Packet Radio Node Program), and the nodejs binary has been renamed from node to nodejs.
So, at least on that machine, you'll have to instead use:
nodejs example.js
Also, you might use cURL instead to test as ping isn't an HTTP client.
curl http://127.0.0.1:1337/
And, as others have already noted, you'll have to include the specified port (.listen(1337,) in the URL since it's non-default.

Related

starting a node.js page on localhost - the site can't be reached

I'm on Chrome / Windows 7 and trying to make my first node.js steps using this tutorial:
https://www.w3schools.com/nodejs/nodejs_get_started.asp
So myfirst.js is:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Going on http://localhost:8080/ I'm getting the error:
This site can’t be reached localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
Firewall is completely off.
Any help?
You need to initiate myfirst.js with node, on the CLI by typing the command for it to run
node myfirst.js
check your firewall
you must edit in firewall system Node.js as public
enter image description here
enter image description here
server.listen(port, hostname, () => {
console.log(Server running at: http://${hostname}:${port}/);
});
port=8080
hostname=127.0.0.1 or 192.168.1.x(your ip)
maybe 8080 is use by other software
I had the same problem but then I installed the pug inside the folder I was working on (basically the folder I named for my website) again and it worked. (idk why did I have to install the package inside the folder as it was installed already outside that folder)

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

Using Node on Network Server

How does node work? I have installed node on the server. On the same network is my work computer where I have git bash and my project files. Am i unable to run my files on my work computer as long as I call the right port number? I am running the code below on my local machine calling the ip address and port number. But then I am getting this error. Error 0x2 starting node.exe index.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!n');
}).listen(1337, 'ip');
console.log('Server running at http://ip:1337/');
Normally this is due to a corrupted binary, I would reinstall both git and node to fix problem.

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

Resources