DEV Community

Cover image for Getting started with Node.js API
Renato
Renato

Posted on • Updated on

Getting started with Node.js API

Node.js API development powered by Express

Note: this post is not recomended for SSR or SR developers due to its simplicity.

Let’s start with a brief conceptual review πŸ’ͺ

First of all let’s talk a little bit about client-server architecture. This is a strongly used model to define a way to communicate one or more parts that request some services (clients) and one or more service providers (servers). First of them could be a website or in fact a mobile application, second of them could be a SOAP webservice, Rest API, among others.
We are going to explain one specifical server service called Rest API, in this opportunity applied with Node.js

Express at a glance πŸ‘€

This amazing framework will help you build a robust Node.js Rest API. It’s important to know that there are other options to take into account like Sails, Meteor, Happi, among others. In this case we will use Express which is the most used one but it doesn’t limit you to use other one. You can read the whole Express documentation here

Requirements πŸ› 

  • Node.js.
  • Nodemon (Optional), used to reload the Rest API when a change is introduced on your code. You can install it with npm globally: npm install -g nodemon, also you can use it with npx.

Installation βš™οΈ

Once the repository is cloned you should install npm packages to be able to run it. To achieve this, just open a terminal on root directory and run the following command:

    npm install

Let’s start with some code πŸ’»

First of all, I will present the folder structure I chose. That’s so simply and I didn’t include some concepts like services or database acceses.

.
β”œβ”€β”€ .env
β”œβ”€β”€ .eslintrc.json
β”œβ”€β”€ .gitignore
β”œβ”€β”€ README.md
β”œβ”€β”€ package-lock.json
β”œβ”€β”€ package.json
└── src
    β”œβ”€β”€ common
    β”‚   └── error.js
    β”œβ”€β”€ index.js
    β”œβ”€β”€ middlewares
    β”‚   └── errors.js
    └── routes
        β”œβ”€β”€ index.js
        └── public.js
  • .env file contains environment variables and you must add this file because it won’t be pushed due to .gitigonore file. We will use API_PORT variable, then you need to add it just like API_PORT=20000 where 20000 is your desired port.
  • .eslintrc.json is used to follow some basic coding rules. You can use it or just ignore it.
  • .gitignore to defined everything you don’t want to push to git repository. Here is so import to add node_modules.
  • README.md just to show some information about the project.
  • package-lock.json keeps the packages dependencies tree tracked.
  • package.json has every needed dependency for this project.
  • src folder where will leave our core code.
    • common folder to define common functions used along whole project.
    • index.js that contains main API configuration.
    • middlewares as the name says, contains every API middleware. In this case we only will use just one for error handling.
    • routes folder where the API routing is defined. Here you can find every endpoint of the API.
      • index.js works like a routes mixer, just that.
      • public.js contains the unique endpoint we have.

Running ▢️

Once npm packages are installed you will be able to run the API. If you have installed nodemon you can run it with:

    nodemon ./src/index.js

Otherwise:

    node ./src/index.js

After this you should show a message like this on your terminal:

Node.js API listening on port: {Your port defined on .env}

At this point, you can go to your favourite browser, put http://localhost:{yourDesiderPort}/api/v1/en and check that the response will be:

{
    "success": true,
    "message": "Node.js API - Hello world"
}

Available endpoints βœ”οΈ

  • GET /api/v1/:lang where lang is related to a language code. The possible values are ['en','es', 'it', 'fr']. This endpoint will return a specific message depending on the language code sent.

Github repository link

You can access to the full code in this Github link

Thanks for reading! πŸ™Œ

Top comments (0)