Tips for Creating Nodejs REST APIs

Published on Nov 22, 2017

3 min read

NODEJS

Originally Published at Codeburst.io

In this article, I am going to offer you some tips for writing REST APIs in Nodejs for a production level application. Writing RESTful APIs with Nodejs is one of the most popular use case using the JavaScript server side platform.

Use HTTP Methods

🔗

CRUD operations are basis of any API. In most applications you will be either:

  • Creating new records
  • Display them on a front end client aka reading a record from the database
  • Updating an existing record
  • Or deleting an existing record

Record here stands for anything that goes into the database. Afterall, an API is just way a to communicate from the user interface to database.

To develop an API that consist of CRUD operations, you must consider using correct HTTP method with the suitable endpoint:

  • POST /record for creating a new record
  • PUT or PATCH /record/:id for updating an existing record
  • GET /record/:id getting a single record
  • GET /record getting a list of all records
  • DELETE /record/:id deleting a single existing record

Use HTTP response status codes

🔗

You must consider using an HTTP status code if anything fails when serving a request.

  • 2xx if everything works fine
  • 3xx if record was moved
  • 4xx request fails due to client error
  • 5xx request fails due to server error (or API)

You can refer to either of these links for a detailed error code and message along with it.

If using Express, most commonly framework used with Nodejs to create APIs, consider writing your code in this format:

1// in case 5xx
2res.status(500).send({ error: 'Internal Server Error.' });
3
4// in case 4xx, unauthorized
5res.status(401).send({ error: 'Unauthorized. Please check.' });

Consider Creating an API Documentation

🔗

Working as a team or even if individual and having a documented API, in the end will benefit all. Following open-source API documentation projects can help in this case:

Avail options for building APIs in Nodejs

🔗

There are quite a number of frameworks that you can choose from to build your next RESTful API with Nodejs.

Express, Koa, Hapi all can be used for creating browser based applications but Restify allows you to focus on building a RESTful service.


More Posts

Browse all posts

Aman Mittal author

I'm a software developer and a technical writer. On this blog, I write about my learnings in software development and technical writing.

Currently, working maintaining docs at 𝝠 Expo. Read more about me on the About page.


Copyright ©  2019-2024 Aman Mittal · All Rights Reserved.