A simple HelloWorld get route in NodeJS (Without Express)


Node.js is a very popular open-source server environment in recent times which is built on Google chrome's javascript engine V8.

How to create a NodeJS project

Pre-requisites

  • Download NodeJS here and install it.
  • You can write code in any text editor. I prefer Visual Studio Code but there are options like sublime text, Atom, aptana studio etc.

How to start

  • create a folder in your repository
  • Open terminal and navigate to the folder you created.
  • execute npm init, which creates package.json file. package.json file is like a manifest for your application and contains npm packages information which are used in the application development.
  • execute code package.json, to open the package.json file in your visual studio code.
  • Create index.js file to write your code

NodeJS Hello world program without Express (index.js)

let http = require('http');

let server = http.createServer((request, response) => {
  response.writeHead( 200, {'Content-Type': 'text/plain'});
  response.end('Hello World!!');
});

server.listen(5000, () => console.log("server started on 5000"));

How to run your code

  • switch to your terminal again or you can open terminal in your VS code itself.
  • Execute the below command
node index.js

How to Test

  • Hit the url http://localhost:5000/ in your browser to see the Hello World!! message