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 it’s’ just a simple API.

Lets’ start with a brief conceptual review ?

First at all let’s talk a little bit about client-server architectu…


This content originally appeared on DEV Community and was authored by Renato

Node.js API development powered by Express

Note: this post is not recomended for SSR or SR developers due to it’s’ just a simple API.

Lets’ start with a brief conceptual review ?

First at all let’s talk a little bit about client-server architecture architecture. This is a strongly used model to define a way to communicate one or more parts who 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 specifically server service called Rest API, in this opportunity applied with Node.js

Express at a glance ?

This amazing framework will help you to build a robust Node.js API. It’s important to know that there are other options to take 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 o root directory and run the following command:

    npm install

Let’s start with some code ?

First at 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. After the

.
├── .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 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 tracked the packages dependencies tree.
  • 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 said, 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 some 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! ?


This content originally appeared on DEV Community and was authored by Renato


Print Share Comment Cite Upload Translate Updates
APA

Renato | Sciencx (2021-09-02T20:46:25+00:00) Getting started with Node.js API. Retrieved from https://www.scien.cx/2021/09/02/getting-started-with-node-js-api/

MLA
" » Getting started with Node.js API." Renato | Sciencx - Thursday September 2, 2021, https://www.scien.cx/2021/09/02/getting-started-with-node-js-api/
HARVARD
Renato | Sciencx Thursday September 2, 2021 » Getting started with Node.js API., viewed ,<https://www.scien.cx/2021/09/02/getting-started-with-node-js-api/>
VANCOUVER
Renato | Sciencx - » Getting started with Node.js API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/02/getting-started-with-node-js-api/
CHICAGO
" » Getting started with Node.js API." Renato | Sciencx - Accessed . https://www.scien.cx/2021/09/02/getting-started-with-node-js-api/
IEEE
" » Getting started with Node.js API." Renato | Sciencx [Online]. Available: https://www.scien.cx/2021/09/02/getting-started-with-node-js-api/. [Accessed: ]
rf:citation
» Getting started with Node.js API | Renato | Sciencx | https://www.scien.cx/2021/09/02/getting-started-with-node-js-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.