• Building a simple Web API using Node.js



    Have you been working with JavaScript Front-end technologies and eager to learn JavaScript for Full Stack Development ??  If yes, you are in the right place. JavaScript become an an essential technology that every developers should know in this era. In this article, I'll show you step by step to create a simple RESTful Web API using Node.js & Express.js by building a Web Service.

    Prerequisites :

    • Basic Knowledge of JavaScript
    • Basic of ES5 & ES6 Concepts
    • Node Installed (https://nodejs.org/)
    • IDE (VS Code)
    • Postman Installed

    What is Node.js ?

    This is a question that raises to most of the developers when they new for the Node environment. 

    Node.js is Not a
    • Programming Language
    • Framework
    Node.js is a
    • JavaScript Runtime Environment
    • Built on Chrome's V8 Engine
    • Its Free & Open Source
    • Written in C++
    • Its runs on various platforms (Windows, Linux, MacOS)

    So Node is allows you to run the JavaScript codes for server communications. The V8 engine in your browser takes your JavaScript code and converts in into a faster machine code. So Machine code is low-level code which the computer can run without needing a interpreter. 

    Why Node.js

    • It's fast & efficient
    • Highly Scalable
    • Event drive, no-blocking I/O model
    • Asynchronous
    • Popular in the industry
    • Same language for both client & server-side

    What is Express.js ?

    Express.js is one the the most popular framework for back-end development. It's highly customizable using middleware. Also Express JS us very easy to use and learn. Express.js basically helps you to manage everything from routes to handling requests & views. 



    What is REST API ?

    REpresentation State Transfer (REST) is an architectural style or design for APIs. Also it is a set of rules that developers follow when they create their API. These rules states that you should be able to get a piece of data (resource) when you link to a specific service URL. 

    Each URL is called Request while the data sent back to you is called Response

    Setting Up the Project

    Once you installed Node.js on your PC go the the CMD and type following commands to make sure its installed successfully.
    node -v
    npm -v
    Create a new folder and go the directory from CMD or VS Code Terminal and initialize new Node.js Project.
    npm init

    Fill the requested information to your requirements. Once it done you will get a new file created called package.json as below.













    Now we can install the express.js web framework by running the below command.

    npm install --save express

    The --save flag is used to edit your package.json file and add express as a dependency. After the installation complete, open up our package.json to see express is listed under dependency. By doing this you could send just your code and package.json file to your friend without the node_modules. Your friend can install all the necessary modules by running the npm install command.

    Creating the Application

    Now its time to code your application. Create a file called App.js and add the following code.


    Congratulations..! You just made your first useless express server. So lets make it something useful. Now you can start the server by running node app.js command & your app will be accessible using http://localhost:3000. But it won't get you anything because We have coded to start our server but we haven't implemented the REST protocols yet to listen to any events.

    Making Request Handlers 

    A Server receives requests, process them and returns a response. So you need to use the routes to handle this requests. The main requests types are GET, POST, PUT & DELETE.

    Let's create a simple GET request that returns a list of users. Modify your app.js file shown as below.

    This simple function makes the express application to use the url handle "/users" to trigger the callback. This callback accepts two parameters, req is the request body which carries information about request & the res is the response body which is used to handle the response functions like .render() to render templates and .json() to return JSON data.

    Running the Application

    To run your application, use the below command.
    node App.js
    If you get "Server running on port 3000" in your Terminal, it means our app is now successfully running. Open your browser and enter http://localhost:3000/users. You will expect to see something like a array of names. /users is the route that we created to send some user's name in JSON format. 

    This is the awesomeness of REST. There is no state between the client and the server. There is no any web pages server to parsed the date. This gives you a complete freedom. All you need to do is write some logic on a specific URL that connects to a Database, use the logic to process the data. You can make your client application any any technologies. It could be a Java Android Application, Ardunio Project or Angular Project. You just need to iterate and display the data anywhere you want. 



    Testing the API

    Let's test our API using POST Man App to make sure that the URL gives the response.


    Where to go from here ?

    With this introduction explore more about Node.js and build some applications. You can build high scalable efficient applications using Node,js, Express & MongoDB. Try to build MEAN (MongoDB, Express, Angular, Node.js) or MERN (MongoDB, Express, React, Node.js) Applications.  

    Some of my Node.js Projects : 


    Share with your mates & Thank You..!


  • 3 comments:

    Thank You

    ADDRESS

    490, Zam Zam Road,
    Maruthamunai,
    Sri Lanka.

    EMAIL

    safnaj99@live.com

    MOBILE

    +94 777 97 42 07