POST Routes
A POST route answers a send ask at one address. A send ask carries new data to the server.
A POST route
This route answers a send ask at /notes.
app.post("/notes", (req, res) => {
res.status(201).json({ message: "Note added" });
});Send a note
This command sends a POST ask to the server.
curl -X POST http://localhost:3000/notesExample
const express = require("express");
const app = express();
app.post("/notes", (req, res) => {
res.status(201).json({ message: "Note added" });
});
app.listen(3000, () => {
console.log("Notes API running on http://localhost:3000");
});Key points
- A POST route answers a send ask at one address.
- Use app.post with a path and a function to make one.
- A send ask carries new data to the server.
- The server answers with a status code.
- A status code is a number that names the result.
- Use 201 when a POST ask makes something new.
In this example, the app.post line makes a POST route at /notes. The route function answers with a short message. The status call sets 201, the code for a new thing. The listen line starts the server on port 3000. A POST ask to /notes gets the message back.
Note
A POST route can change stored data, so asking twice can add twice.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does a POST route do?