Http Serving with node.js over the web - node.js

This is a very basic question. But I have looked and can't seem to find any tutorials that walk through this step. Everything either stops just before this step OR starts just after it.
I have launched an AWS server (Windows_Server-2016-English-Full-Containers-2016.10.18 (ami-d08edfc7)) and installed node in the default directory.
I have put a file into the following content:
var http = require('http');
const PORT=8080;
function handleRequest(request, response){
response.end('It Works!! Path Hit: ' + request.url);
}
var server = http.createServer(handleRequest);
server.listen(PORT, function(){
console.log("Server listening on: http://localhost:%s", PORT);
});
I then open CMD, navigage into the directory where node is installed and run the program with:
node myServer.js
Next I open a browser and navigate to http://localhost:8080 and I am served some content. Terrific.
My question is how do I go about making a request of that newly installed server from another machine over the internet. My primitive guess was to simply navigate to the AWS machine's public IP, as displayed in the AWS console and include the port number.
So for example if my IP were 55.173.140.15 I would type in the address http://55.173.140.15:8080 and expect to see the page. That is not working. So what configuration step am I missing?

Related

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

Browser downloads the file instead of showing html

I have developed MERN app in my local system. I am trying to host it AWS-EC2 (Free Tier).
Never thought deploying would be so painful.(Well, i am beginner to Node. Earlier i have worked with php. I found it easier to integrate)
I referred this article to install Node and Express. Node is successfully installed. I created sample file 'test.js' with following code:
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'test/plain'});
res.end('Hello world!\n');
}).listen(port);
console.log('Listening on port',port);
After executing
node test.js
the browser downloads a file with no extension. Opened in editor and it has 'Hello World' in it. Nothing else. I am pretty sure i have followed all steps properly. But still i ended up 'Downloading the file'
Can anyone help me with this issue?
BONUS QUESTION : How do i deploy MERN app in AWS EC2?
Have a nice day
Thanks
try this (you forgot to add the listen callback, and a typo in content type):
var http = require('http');
var port = 9000;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello world!\n');
}).listen(port, err => {
if (err) throw err
console.log('Listening on port',port);
})
Use appropriate content type for your html page.
res.writeHead(200,{'Content-Type':'text/html'});
For a viewable html code, text/html should be set as content type instead of test/plain.

Not able to run node.js program in command prompt

error on executing the programI am beginner to node.js .I have followed all the steps from the below blog:
http://blog.teamtreehouse.com/install-node-js-npm-windows
I got the node version and npm version too.
But not getting the output when I try to run the js file from cmd.
console.log('Node is installed!');
the content of the js file is mentioned in the above line.
Please help me out!!
Thanks in advance
Its Seems there is no hello.js in c drive or in wrong directory
Goto any folder and make your hello.js and then navigate to that folder in cmd then write
node hello.js // .js is optional
in the picture above you can i receive error helloo cant find module becoz i dont have any file with name helloo.js
suppose your file is in D drive then goto D drive and wirte
Your can make simple hello.js server
//Lets re quire/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT=8080;
//We need a function which handles requests and send response
//Create a server
var server = http.createServer(function (request, response){
response.end('It Works!! Path Hit: ' + request.url);
});
//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);
});

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

http server listening in old port

First i installed the node js with webmatrix and ran a sample node js app. the app was assigned a random port. http://localhost:62369/. After that i installed the express module. As said in their doc. i wrote,
var app = express();
app.get('/',function (req, res) {
res.send('hello world!!');
})
app.listen(3000);
Then i restarted the server. The launched browser was still pointing to http://localhost:62369/ instead of port 3000. Moreover http://localhost:3000/ was not working.
I suggest you to run this code so you can see if you have any problem on saving the code with your IDE:
var app = express(),
port = 4555;
app.get('/',function (req, res) {
res.send('hello world!!');
})
console.log("Server is running on " + port);
app.listen(port);
After that, you need to change the port variable only. It's helpful if you comment what you see after running this code on the console.
make sure that you've saved your code in your file (open it with another editor, maybe something's wrong with your editor), close the command line window and open it again. try to run server. I'm sure the problem is not because of node or express. Try to check everything again.
And also run your server with command line:
cd path/folder
node myFile
I don't know what are you using to run server, but if it's something with UI (in comments you mentioned a click) it can cache your code or something like that. So it's safer to run with commend line.

Resources