No response on localhost from nodejs server - node.js

I have just begun with NodeJs and was trying a commonly found basic example of a server -
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
console.log('Hi there !');
Now when I run node example.js using cmd, I get the correct message Hi there !. However, when running on localhost:8888 or 127.0.0.1:8888, I get This webpage is not available.
I also have XAMPP installed but from what I know that uses port 80.
Also my installation directory and my code directory is different. Is that causing a problem here ? Any help appreciated

Related

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.

How to create a localhost in Node.js? And why mine is not working?

I'm not able to create a localhost for Node.js. Following are the thing I had completed:
I had created a file named server.js and the code is as follows:
var http = require('http');
http.createServer(function(request, respone){
respone.writeHead(200, {'Content-type':'text/plan'});
response.write('Hello Node JS Server Response');
response.end("Hello Sowmay Response ended");
}).listen(7000);
Then I had run this javascript script file on node.js cmd prompt as follows:
C:\Users\Guest\Desktop\First-Node>node server.js
After that I tried opening the local host on my windows PC from this link: http://localhost:7000
Result:
This site can’t be reached
localhost refused to connect.
Where I'm going wrong?
There is a mistake in your code.
I guess your console would output :
undefined has not a function "writeHead"
Becuase respone.writeHead(200, {'Content-type':'text/plan'}); lack a 's'.
Please use:
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200, {'Content-type':'text/plan'});
response.write('Hello Node JS Server Response');
response.end("Hello Sowmay Response ended");
}).listen(7000);
and try again

How to access to the application through browser?

How can I access my server through the browser once I've set up the server ?
In PHP I simply have to put my files in the www/ folder and then go to http://127.0.0.1/index.php, but how can I do that with NodeJS ?
My server's code is from a tutorial :
var app = require(‘express’).createServer();
app.get(‘/’, function(request, response){
response.send(‘Hello Web Designer’);
});
app.listen(8888);
But whenever I go to http://127.0.0.1:8888/ i get a "Problem loading the page". So either my server is not running correctly (hard to tell when the NodeJS console shows "...") or I am not accessing it correctly.
What can I do please ?
you have to send a http head and end the response.
Try this code.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('hello world');
res.end();
}).listen(8888);
(Pseudo Code)
Also don't use any frameworks to learn how node works.
You can run you index.js now and call localhost:8888
Did you start the server? The apache server runs php, in the nodejs world, there is a javascript runtime that needs to be started.
Try node server.js from the command line (or whatever your file is called) to get it to listen and serve incoming requests.

listen() doesn't return?

I'm trying to get started with Node, and have hit a hump. When I try
to run the below (same example I've seen everywhere) the "starting..."
line executes, as well as "created.", but it seems like the script gets stuck on the next line, and never prints "started." (and the server doesn't work, tried via a
browser, telnet, curl, etc). Any advice on how to debug?
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
console.log("starting...");
var s = http.createServer(onRequest);
console.log("created.");
s.listen(8888);
console.log("started.");
Debian GNU/Linux 6.0.1 (squeeze), 2.6.39-x86_64
Compiled node.js 2011.07.19 v0.4.10 (stable) from source.
Thanks!
Actually it looks like listening has some problems with socket access. Try to change port number, turn off the firewall or run node with root access. Maybe it should help;
In other way try it with python or smth like that;
Change the port number to 3000 AND run as root. Ensure port 3000 is NOT being used (i.e. use netstat). You should see the "started" message. Otherwise, I can't really help. I'm running Node 0.4.10 on Ubuntu and I copied/pasted your code; it runs perfectly. If it still doesn't work, reinstall Node.

Why my http server I created not working with http://myserver:port/?

Recently I installed node on new server , and create this simple program.
var sys = require("sys"),
http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.end();
}).listen(8080);
sys.puts("Server running at http://localhost:8080/");
When I run this in browser http://newserver:8080/ version 0.4.10 not working .
http://oldserver:8080/ working .version 0.5.0 pre .
Why this happening?.
You've referred to a new and an old server, which suggests to me you're not running this from localhost. In that case, have you verified that both servers have the same firewall rules? Specifically, is port 8080 allowing external traffic on the new server?
Is someone already listening on 8080? Try telnetting to that port and see if it works.

Resources