I have an AWS EC2 instance which I am using for my node js application. I cannot access any application page on the server.
I wrote this simple code solely for testing purposes but I cannot access even this from my browser.
var express = require('express');
var app = express();
app.listen(3000, ()=> {
console.log('listening');
});
app.get('/',(req,res)=> {
res.send('hi');
});
On navigating to http://:3000, I should be able to see "hi" written but the request times out.
Here are my security group configs :
Solved the problem with some help.
Since the port available to me was port 80, so I just forwarded the port 8080 to port 80 via. port forwarding and it worked out.
Sharing the link from where I found the solution:installing nodejs and forwarding port on aws
My code (shown below) is a bit different, but I was having the same problem with connecting from a remote browser:
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}/`);
});
What I had to do was replace const hostname with the complete AWS server IP: ec2-xx-xx-xx-xx.compute-1.amazonaws.com:3000/
Make sure you have port 3000 open in your security group, or this will not work.
I was then able to connect to the NodeJS server from my browser on my PC.
Hope this helps. The corrected code is shown below (replace the x's with your actual IP address. You can get this on your EC2 dashboard.
const http = require('http');
const hostname = 'http://ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com/';
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}/`);
});
I got the code for this NodeJS server from:
https://websiteforstudents.com/install-the-latest-node-js-and-nmp-packages-on-ubuntu-16-04-18-04-lts/
Related
After deployment of my NodeJS app to Google Cloud I get the 500 Server Error when trying to access it on the given address.
The app works when running it locally and also seems to be running on the server as I get this output when running:
gcloud app logs read
2022-12-01 10:48:21 default[20221201t114541] Server running at port:3000/
The app is just a simple one for testing:
const http = require('http');
const port = process.env.port || 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, () => {
console.log(`Server running at port:${port}/`);
});
I've also added firewall rules to allow traffic on port 3000.
This question already has answers here:
Containerized Node server inaccessible with server.listen(port, '127.0.0.1')
(2 answers)
Closed 8 months ago.
I am building container with node.
I have two type of codes. I think two codes very simple and similar. and package.json and Dockerfile is same. just different code.
but I can connect app in container. but the other one can't connect app in container.
This code is working well in container.
const express = require('express')
const PORT = 3000;
const app = express();
app.get('/',(req,res)=>{
res.send('Hello World');
});
app.listen(PORT);
console.log('Running');
but This code is not working What wrong with this? this is node official sample 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');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
please tell me what problem is.
Thank you in advance
The sample code works when run on the host machine, but not when run in a container.
The issue is that the sample code binds to the 127.0.0.1 IP address. That means that it'll only accept connections from localhost.
In a container, localhost is the container itself. So when you try to connect from the docker host, it looks like the connection comes from another machine and the connection is rejected.
To fix it, you need to make it accept connections from outside the container. You can do that by changing the hostname variable to 0.0.0.0 like this
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
You can also leave out the hostname variable on the server.listen call as you've done in the first program. The default value is 0.0.0.0, so leaving it out also works.
I just create my oracle cloud ubuntu instance and I want to run a hello world Nodejs application and make it accessible to the internet.
But the problem is when I try to access it from my browser (http://public-ip-address:3000) doesn't work works.
I followed this instruction " https://gist.github.com/bradtraversy/cd90d1ed3c462fe3bddd11bf8953a896 "
but it doesn't work too
this is my app.js
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');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
I am new and want to start using Node.js.
I try to run an “Hello, world!” on Synology:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3001;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
This code runs:
~$ node app.js
Server running at http://127.0.0.1:3001/
But nothing was found when accessing Synology:
ERR_CONNECTION_REFUSED
I found the instruction:
Open Firewall-Port on Synology depending on which port you are going to run your xxx (e.g. 3001)
OR
Create volume1/path_to_your_app/app.js and let the http server listen to port 8080.
Where is this setting on Synology?
Your 127.0.0.1 needs to be the IP address of your NAS. If you are on your router locally and you have your NAS set to for example 192.168.3.10, then your code would be:
const hostname = '192.168.3.10';
const port = 3001;
Then in your web browser you would type 192.168.3.10:3001 after running app.js
-> go to Control panel, then Firewall, Firewall-profile. There you can edit your firewall-rules and alternatively add or remove rules.
I hope this is what you are searching for. Otherwise write what you are looking for
I can't figure out how to get my server to respond with Hello World.
I don't even know what IP address is. Is the ip listed on the tab of my terminal it?
I just created an EC2 environment with the default Node.js template.
Do I need to setup more things beforehand?
https://i.stack.imgur.com/4m85x.png
Try the solution bellow and let me know if you need any explanation:
const http = require("http");
const port = 3000; // make sure the port number is not used
const requestHandler = (req, res) => {
req.on('Error occurerd', (err) => {
console.error(err);
res.end();
});
res.end("Hello from AWS Cloud9!");
}
const server = http.createServer(requestHandler);
server.listen(port, () => {
console.log(`Server is listening on port ${port}!`);
// Run your script then copy past this url in your browser 127.0.0.1:3000
});