Cannot access Nodejs/Express Server on macOS High Sierra from other computer - node.js

I started an simple express server on MacOS High Sierra. localhost:3000 and 127.0.0.1:3000 work fine. However, when I accessed the server via http://192.168.x.x:3000 from another computer, I didn't get any response (ERR_EMPTY_RESPONSE).
I tested the server on another MacBook (MacOS Sierra) and a Windows computer, everything works fine.
So I suppose there are something wrong with MacOS High Sierra.
Any help would be welcome. Thanks in advance.
Here is the code :
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))

UPDATE MacOS
I have tried to reinstall node, npm, change share settings, but they don't work, neither.
Finally, I updated MacOS (10.13.4 --> 10.13.5) and now other computers can access my server like a charm.
----------Update -------------
After a few (happy) day, the issue raised again.
Fortunately , now I pretty sure that ESET EndPoint Security was the problem since it's the last application I installed.
So I open the ESET firewall and create a new rule which allow local network address to connect via TCP&UDP.
The server once again works like charm.
Hope this help the other.

Refer to https://nodejs.org/en/docs/guides/getting-started-guide/
Once you have installed Node, let's try building our first web server. Create a file named "app.js", and paste the following code:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
After that, run your web server using node app.js, visit http://localhost:3000, and you will see a message 'Hello World'

Related

Node Js hello world Program is not being displayed in localhost

const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
enter image description here CMD
enter image description here local host
I need to run node js successfully and see the hello world output on local host
That code seems to be OK.
Please try http://127.0.0.1:3000
Or try to move your project to different folder, I see it is placed in One Drive folder ? Maybe some permission issue ?
I hope you also tried restart your machine if you just installed NodeJS before trying this out.

Accessing node server through url

So I have this script
const hostname = 'localhost';
const port = 2525;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
File named nodeTest.js and I have all this on a webhost where I also have my domain. Now running this script through ssh "node nodeTest.js" creates server and I can access it with another ssh terminal with curl call. I get response "hello world" as I should.
Now the problem is I cannot access it normally through my domain link.. like somethingsomething.com
I also have option to create node application through a cpanel but when I create it i get no response going to specified link
I just started with nodejs so maybe I am missing something obvious. There is also help site on my webhost web that has tutorial how to set this up and I do as they say but i get no console.log msg.
I also tried setting server to 8080 port but I get error already in use.
Would appreciate if someone would point me in the right direction.
I figured it out.. with the help of this site
https://www.a2hosting.com/kb/cpanel/cpanel-software/create-application-with-nodejs-selector
I was missing package.json part

Running Node.js server on windows

How can I host node.js application on windows server?
I suggest you use iisnode to achieve your requirement.
You should firslty install the IIS URL Rewrite extension, node.js, iisnode.
After you installed above things, you could find you IIS Modules contains the IISnode feature, then you could run your node.js application on IIS as other web application.
More details about how to host node.js application, you could refer to below article.
https://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx
https://www.simplymigrate.com/2017/04/11/internet-information-server-iis-node-js-in-producton-iisnode/
1) install nodejs (Download from here)
2) write your server program (it should contain a proper node.js listener code)
3) run your code; open a Powershell or CMD and type the following command:
node my_server.js
you can also refer to these links:
Install Node.js and NPM on Windows
PM2 | process manager for Node.js
P.S:
Here is a very very simple node.js server code (from node.js docs here):
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Hope it helps!

Output in browser using Express Nodejs

I install nodejs with express in server (subdirectory in ftp,path is /var/www/html/admin)
and in admin folder i created file "app.js" which is working in xshell fine and showing message in console
here is my app.js
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
but now i want to show result in browser,how can i do this
i tried with following urls but not working for me
http://myurl.com/admin:3000
http://myurl.com/3000/admin
First get ip address of your FTP server.
Then connect http://<FTP_SERVER_URL:3000 to see 'Hello World' you wrote in the code.
To browse /admin folder you mentioned and if your express app is running on root directory, try http://<FTP_SERVER_URL:3000/var/www/html/admin
You can use document.write() method to write something to the DOM object( or in layman's language you can call it browser screen).
Try using the following:
app.listen(port, () => document.write(`Example app listening on port ${port}!`))

Bridged Adapter Issue Windows 10 VB 5.0.12 Ubuntu 14.04

I'm learning Node.js and I've installed VirtualBox and there ubuntu server 14.04. Node.js is installed as well on ubuntu. I'm doing an exercise in which I created a server which is accessible from guest_localhost:3000 (in ubuntu). The thing is I've not installed any GUI (and I wish to continue in that way). I want to test that the server I created using Node.js is running. To do that I just need to go to server_localhost:3000.
Apparently everything is ok (for example ping host to guest ip), but for some reason it´s not working.
ping ok __ not working
`var http = require('http');
var handleRequest = function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to Node Essential Training\n');
};
var server = http.createServer(handleRequest);
server.listen(3000, 'localhost');`
I'll appreciate any help.
Thanks in advance guys.
If you do not have any GUI to visibly verify your page layout, you should be able to access it from your windows machine if they are in the same local network by simply accessing the url http://your_ubuntu_local_ip:3000 where your_ubuntu_local_ip is the local ip your ubuntu machine has internally in your local network. You can find this ip by typing ifconfig in your ubuntu terminal and looking for the ip the network adapter you are using has.
It's solved. The problem was in the code to create the server after all. As I'm learning JavaScript and Node.js I'm watching a tutorial in Lynda.com. The code which doesn't work comes from Lynda. The other from this url: http://blog.modulus.io/build-your-first-http-server-in-nodejs
// THIS CODE DOES NOT WORK
//var http = require('http');
//
//var handleRequest = function (req, res) {
// res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Welcome to Node Essential Training\n');
//};
//
//var server = http.createServer(handleRequest);
//
//server.listen(8080, 'localhost');
//THIS CODE WORKS
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=3000;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});

Resources