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

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

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)

Nodejs hello world

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);

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.

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

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.

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