Node Js hello world Program is not being displayed in localhost - node.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}`)
})
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.

Related

Trouble setting up simple local host using nodejs with npm package express, any reason why program will not run

This is the method i followed, installed express from npm. Create new folder in cmd using mkdir, go to that directory open in vs code, create file called index.js
used the following code:
const app = express();
app.get('/', (req, res) => {
res.send('Hello World')
});
app.listen(3000, () => console.log('Listening on port 3000...')
when running the file from cmd using
node index.js
nothing happens not even an error code, any help would be appriciated

CRA Socket.io request returns net::ERR_CONNECTION_TIMED_OUT

Hello. I've spent some time without luck trying to understand the problem here.
I've looked through each Question on StackOverflow which seems to deal with the same problem, though nothing has worked so far.
I have a simple chat app built using Create React App and Socket.io (which runs fine on localhost), but when deployed to my Node server I'm receiving ERR_CONNECTION_TIMED_OUT errors and no response. The website itself runs fine, but when I make a call to my Socket.io server, but errors.
I'm guessing this is down to my lack of knowledge with how Node and Socket.io want to work.
Some info:
server.js
const path = require("path");
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const port = 8080;
http.listen(port, () => console.log(`http: Listening on port ${port}`));
const io = require("socket.io")(http, { cookie: false });
app.use(express.static(path.join(__dirname, "build")));
app.get("/*", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
io.on("connection", (socket) => {
console.log("New client connected");
// Emitting a new message. Will be consumed by the client
socket.on("messages", (data) => {
socket.broadcast.emit("messages", data);
});
//A special namespace "disconnect" for when a client disconnects
socket.on("disconnect", () => console.log("Client disconnected"));
});
client.js
....
const socket =
process.env.NODE_ENV === "development"
? io("http://localhost:4001")
: io("https://my-test-site:8080");
socket.on("messages", (msgs: string[]) => {
setMessages(msgs);
});
....
docker-compose.yml
version: "X.X"
services:
app:
image: "my-docker-image"
build:
context: .
dockerfile: Dockerfile
args:
DEPENDENCY: "my-deps"
ports:
- 8080:8080
Dockerfile
...
RUN yarn build
CMD node server.js // run my server.js
...
UPDATE: I got around this problem by making sure my main port was only used to run Express (with socket.io) - in my set up that was port: 8080. When running in the same Docker container, I don't think I needed to create and use the https version of the express 'createServer'.
This looks like you forgot to map the port of your docker container. The expose statement in your dockerfile will only advertise for other docker containers, which share a docker network with your container, that they can connect to port 4001 of your container.
The port mapping can be configured with the -p flag for docker run commands. In your case the full command look somehow like this:
docker run -p 4001:4001 your_image_name
Also, do you have a signed certificate? Browser will likely block the conneciton if they do not trust your servers certificate.
I got around this problem by keeping just one port available (in my case :8080). This port is what express/socket.io is using (originally I had two different ports, one for my site, one for express). Also, in my case, when running in the same Docker container, I didn't require the require("https").createServer(app) (https) version of the server, as http was sufficient.

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}!`))

Cannot access Nodejs/Express Server on macOS High Sierra from other computer

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'

NodeJS Deployment confusion between localhost and other domain?

I have a simple program which executes fine in localhost.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var port = process.env.PORT || 8080;
var host = "127.0.0.1";
var server = app.listen(port, host, function(){
console.log("Server running in : ",host ," with port no : ",port);
});
Trying to deploy the same to heroku using codeship. Everything is building perfect except the last line of deployment test command i.e node index.js which in turn is referring to 127.0.0.1 and stops deploying. May i know do i need to change something here for the host and port address
Just don't provide a host:
var server = app.listen(port, function() {
console.log('Server listening on', port);
});
(This implies, "accept connections on any host, on this port", vs what you're trying which implies, "accept connections on 127.0.0.1 on this port")
Try to run your app on localhost with the help of foreman that is a part of the Heroku Toolbelt. For instance:
foreman start web
You should see your app running on http://localhost:5000 or the port you have specified in your package.json file.
Suggest this link for further queries:
prerequisites to deploy a node app on Heroku?
I was able to host it successfully following through this steps
As suggested by #hunterloftis, i removed hostname.
More importantly, Procfile was missing,so added it and deployed successfully

Resources