How to Create Your First Node.js Application?

create your first node js application
by

Like everyone else in software development heard about nodeJS at least once due to its popularity among developers and enterprise organizations. Let’s put those statistics to work and try our self is it that easy and robust?

Step 1: Installation

I am starting up fresh here. So let’s install node js into our machine. You can skip this step if you already have it installed.

These are the things we’ll need to develop in node.js

  1. Node.js setup
  2. VScode

Goto https://nodejs.org  and select version you like to install. I recommend using LTS(Long Time Support) versions so that I don’t run into the environment-related issue in the future. It will automatically start installing the version based on your operating system.

VScode is a lightweight editor from Microsoft. I’d recommend it but if you have any other editor already installed it will work as well.

Step 2: Setup a directory

Let’s create/find a find directory to work on. I will name it a tutorial for now. and open terminal/command prompt window. open the same directory in VScode(or your editor) as well.

Step 3: create a file and start coding

Select File > New file and save it as index.js

Just to test the setup we will do Hello world!

console.log("Hello world !!");

Let’s test it.

It works !!

Let’s do something complex and useful as a project. Let’s create an endpoint and call it.


Adding some constants
const http = require('http');
const port = 3000;

A request handler function
const requestHandler = function() {
  console.log(request.url);
  response.end('Hello Node.js Server!')
};

Creating a server
const server = http.createServer(requestHandler);

Starting the server
server.listen(port, function(err){
    if (err) {
        return console.log('something bad happened', err)
    }
    console.log('server is listening on' + port)
});

So the whole file should look like this
const http = require('http');
const port = 3000;
const requestHandler = function() {
  console.log(request.url);
  response.end('Hello Node.js Server!')
};
const server = http.createServer(requestHandler);
server.listen(port, function(err){
    if (err) {
        return console.log('something bad happened', err)
    }
    console.log('server is listening on' + port)
});

Let’s put it to test

It works. The message clearly means we can test it from the browser. So why not?

Well, that’s a winner. Right there.

you can stop the execution using ctrl/cmd + c in cmd/terminal

You can also use libraries like express or hapi or restify to do some advanced routing, but the default/core node js libraries are enough as well.

Summary

By making a simple server using 12 lines of easy code seems easy for developers and if you have a cloud instance, you can comfortably host this on it. So yeah Nodejs is simple and easy.

 

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!


iphone-app-development


Looking to build an app ?