Nodejs Server Creation not running - node.js

iam very new to nodejs. I found difficult with this server creation program
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();
}
http.createServer(onRequest).listen();

You have to mention the port number to listen.
Your code has to have aport number in the listen.
http.createServer(onRequest).listen(8080);
The port number 8080 was already in use....So i used
another port number like 8083....It works...

Related

stripping port number from domain in aws server

http://www.basicspace.org:5000/#/
Here is my url, I want to run the app just on the http://www.basicspace.org without the port number. I have seen lots of tutorials but none got into my head so asking it here for better solutions.
If it is just a single EC2 Instance which is running NodeJS then. If it listens behind and ELB, then PORT source and destination to be configured in ELB.
var util = require('util');
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.write('Hello World');
response.end();
}).listen(80);

node.js 8080 port isn't listening

I'm following the node.js tutorial in here,
http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/
this is the code,
var sys = require("sys"),
http = require("http");
http.createServer(function(request, response) {
response.sendHeader(200, {"Content-Type": "text/html"});
response.write("Hello World!");
response.close();
}).listen(8080);
sys.puts("Server running at http://localhost:8080/");
in here, it says run like this url,
server's ip:8080/
but if i do this,
it just shows, cannot connect to this url.
i opened 8080 port in the server.
===========================
I'm assuming something is screwed up with codeigniter url helper...
The tutorial may be using an incorrect or deprecated method. Replace
response.sendHeader(200, {"Content-Type": "text/html"});
with
response.writeHead(200, {"Content-Type": "text/html"});
and
response.close();
with
response.end();
I Agree with the answer of Third .... make those changes and if it is local use this URL
http://127.0.0.1:8080/
But
If you are running your server not on localmachine but on something like webserver(AWS), You have to let the security of AWS firewall to allow the port to be public on the internet and also remember to use the AWS instance URL
http://AWSinstanceURL:portno/
Use this one
//Lets require/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
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);
});
Now open http://localhost:8080 and u will get your result.

access node.js server from domain name

I'm learning Node.js can created a hello world server, here is the code server.js
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
I can access localhost:8888 from my server machine but I can't access ip:8888 from another machine and I also can't access domainname:8888. What is the problem?

how to send reponse when timeout in node.js http module

On nodejs.org socket.setTimeout, it says
When an idle timeout is triggered the socket will receive a 'timeout' event but the connection will not be severed.
But when I test code like this:
var http = require('http');
server = http.createServer(function (request, response) {
request.socket.setTimeout(500);
request.socket.on('timeout', function () {
response.writeHead(200, {'content-type': 'text/html'});
response.end('hello world');
console.log('timeout');
});
});
server.listen(8080);
The socket is closed immediately after timeout, and no data is replied to the browser. Which is quite different from the document. Is this a bug or is there any tricks dealing socket under http module?
The documentation is indeed correct, however it looks like the http module adds a 'timeout' listener which calls socket.destroy(). So what you need to do is get rid of that listener by calling request.socket.removeAllListeners('timeout').
So your code should look like:
var http = require('http');
server = http.createServer(function (request, response) {
request.socket.setTimeout(500);
request.socket.removeAllListeners('timeout');
request.socket.on('timeout', function () {
response.writeHead(200, {'content-type': 'text/html'});
response.end('hello world');
console.log('timeout');
});
});
server.listen(8080);

node.js beginner - why does this receive 2 responses?

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();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
this writes to console on a request from my chrome browser:
Request received
Request received
Two times? Why?
It was your browser asking for favicon.ico

Resources