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.
My friend Christopher Harrison will walk through what you need to get started.
Note: every single video below runs for less than 5 min, to make the most of your time 😉
- 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
- Interactive in-depth tutorial on Node.js filesystem
- Source code of the demonstrated API
- Express reference
- REST Client extension for VS Code
- Use CosmosDB with a Node.js app
- nodemon on GitHub
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.
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.
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.
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.
To complete our API and make it a full CRUD (Create, Read, Update and Delete) API, we finally have to add the
DELETE endpoints. Let's see how it differs from Read and Create and put the finishing touch to our server.
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!