Before you go, check out these stories!

0
Hackernoon logoCreating RESTful Web APIs With Node.js And Express: You Canโ€™t Mess Up The Right One by@sinedied

Creating RESTful Web APIs With Node.js And Express: You Canโ€™t Mess Up The Right One

Author profile picture

@sinediedYohan Lasorsa

Fullstack Developer & Cloud Advocate @ Microsoft. Code artist, OSS maintainer, JavaScript tinkerer.

One of the main reasons for using Node.js is that you can create quickly RESTful web APIs with it. Express is a popular web framework helping you with this task. Using JavaScript to build servers is often a natural choice for frontend developers looking for a way to reuse their skills and create the APIs supporting their web app, but that's not the only benefit of using Node.js. The huge package ecosystem around Node.js and Express is what makes it shine, allowing to implement a lot of business features with very few lines of code.

My friend Christopher Harrison will walk through what you need to get started, with short videos that run for less than 5 min each.

What you'll learn here

  • What is Express and how it can be used to create web APIs
  • Handle routing with parameters
  • Receive and parse JSON data
  • Use HTTP verbs to create a RESTful CRUD API

Additional learning resources

Introducing Node.js and Express

When you want to create a web API using Express, you'll probably also need other packages as well to help you work more efficiently.

nodemon
for example is a little helper that allows you to automatically restart your server when you make changes to the code. Let's discover what Express can help you with, and what are the packages that you'll want in almost all your server projects.

Create a GET API

One of the first steps that come after initializing the Express server instance is creating a simple

GET
API that. To do that you'll need to use the router to create a new endpoint, and leverage the Express API to create the HTTP response, all with very few lines of code.

Handle routing with parameters

When you're building a REST API, you need to retrieve parameters from the URL so you can extract IDs from it to access specific resources. The router object can help you with that, and by using patterns to define the routes you can make Express to extract the data for you.

Receive and parse JSON data

Express by itself is not enough to create a

POST
endpoint that receives JSON. You'll need to use extra middlewares, like
body-parser
to be able to parse and use data coming from a client web app. Let's take a closer look.

Create update and delete APIs

To complete our API and make it a full CRUD (Create, Read, Update and Delete) API, we finally have to add the

UPDATE
and
DELETE
endpoints. Let's see how it differs from Read and Create and put the finishing touch to our server.

What's next

You should now have the ground to create your own API with Node.js and Express. If you want, you can also take a look at what the final API we built looks like.

If you would like to deepen your knowledge, you can also take a look at this interactive in-depth tutorial on Node.js and Express. Once you start building your API, one of the next steps is usually to plug in a database. You can take a look at this tutorial to learn how to create and connect a CosmosDB instance to a Node.js app. If you've never heard about CosmosDB, it's a hosted distributed database that supports multiples connectors, including MongoDB, a popular choice among Node.js developers.

Thanks for reading!

Also published at https://dev.to/azure/build-a-web-api-with-node-js-and-express-16fk

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.