Create a nodejs app in oracle cloud ubuntu instance - node.js

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}/`);
});

Related

I am building container with node. but I can't connect app in container [duplicate]

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.

Problem with publication Next js project on Digital Ocean droplet

I'm trying to run Next js application on my Digital Ocean Droplet (with OpenLiteSpeed).
I have start.js file with the following content:
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
handle(req, res, parsedUrl)
}).listen(80, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:80')
})
})
When I calling this file from console: node start.js, my site successfully running on port 80.
But when I'm trying to add this file as a start up file in App Server Context Definition, site is not running, and my website just cannot be reached.
But when I'm changing the file to default one (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! From OpenLiteSpeed NodeJS\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://:/`);
});
The website is successfully opening with Hello World.
Listener is successfully setup to port 80:
Grace restart is done. changing ports to 3000 (for example) not helping much: I got same behavior.
What I am doing wrong?
UPDATE:
I found temporary solution:
node start.js & disown
and then close the Terminal.
Please anyone give me drawbacks of this method.

Can't get Hello World with Node.js in Cloud9

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

i have error its show a syntax error in line 1 character 1

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {//i create the server
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');//log to the console
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
//and its dont work its show me a syntax error
//if someone can tell me how to fix this
Don't double click on your js file, you have to launch it using nodejs :
Download nodejs
Open a terminal
Move to your current directory (where the file is located) with the cd command
Type "node yourfile.js"
You will have the output of your program in the terminal.

Can't access nodejs server on aws

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/

Resources