Using Node on Network Server - node.js

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.

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

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.

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

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