Configuring the POST route

Now, we have a very basic server. All we have to do is start configuring our routes, and as I promised, the one we're going to be focusing on in this section is the POST route. This is going to let us create new Todos. Now, inside of your REST APIs, there's the basic CRUD operations, CRUD being Create, Read, Update, and Delete.

When you want to create a resource, you use the POST HTTP method, and you send that resource as the body. This means that when we want to make a new Todo, we're going to send a JSON object over to the server. It's going to have a text property, and the server is going to get that text property, create the new model, and send the complete model with the ID, the completed property, and completedAt back to the client.

To set up a route, we need to call app.post, passing in the two arguments we've used for every single Express route, which are our URL and our callback function that get called with the req and res objects. Now, the URL for a REST API is really important, and there is a lot of talk about the proper structure. For resources, what I like to do is use /todos:

app.post('/todos', (req, res) => {

});

This is for resource creation, and this is a pretty standard setup. /todos is for creating a new Todo. Later on, when we want to read Todos, we'll use the GET method, and we will use GET from /todos to get all Todos or /todos, some crazy number, to get an individual Todo by its ID. This is a very common pattern, and it's the one we're going to be using. For now though, we can focus on getting the body data that was sent from the client.