loading...
Cover image for Build a web API with Node.js and Express
Microsoft Azure

Build a web API with Node.js and Express

sinedied profile image Yohan Lasorsa Updated on ・4 min read

Are you new to Node.js? We created Beginner’s Series to Node.js, a series of short practical videos to get started with Node.js, from installing to debugging, with everything in between. You can look at the announcement here and watch all videos here: Beginner's Series to Node.js.

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.

geektrainer image

Note: every single video below runs for less than 5 min, to make the most of your time 😉

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 a this tutorial to learn how to create and connect a Cosmos DB instance to a Node.js app. If you never heard about CosmosDB, it's a hosted distributed database that supports multiples connectors, including MongoDB, a popular choice among Node.js developers.

Please share any questions or experiences you've had on these topics in the comments. Thanks for reading!


Follow me on Twitter, I would be happy to discuss and take your suggestions!

Discussion

pic
Editor guide