Motyar

@motyar

Freelance Web Developer

Static Web Hosting, made easy

Nov 4, 2013

A basic HTTP server in NodeJs

Node.js has a built-in HTTP server library, making it possible to run a web server without the use of external software, such as Apache etc. It allow more control of how the web server works.

So in nodeJs, You don’t have a server, you have to code web server along with your application code.

This is an implementation of a "hello world" HTTP server in Node.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);

console.log('Server running at http://localhost:8888/');

Put this code in web_server.js, and run


node web_server.js

Now point your browser to http://localhost:8888/. This should display a web page that says "Hello World".

Check full NodeJs course here. Learning NodeJs

Labels: ,




By :