Step 5
Adding POST request to router
Let's create a new Book!
In the REST API world, HTTP GET requests are usually used for when you are requesting information from the server, while an HTTP POST request is usually for when you want to send information to the server. Since we want to send our server information for creating a new book, we will need to handle an HTTP POST request. Let's add this logic in our '/api/books' route:
src/routes/books.js
...
router.post('/', async (req, res) => {
const { title, author } = req.body;
if (!title) return res.status(400).send('Please provide a title');
if (!author) return res.status(400).send('Please provide a author');
const newBook = await Book.create({ title, author });
res.send(newBook);
});
...
The information that we are sending to the server is sent in the body of the request. Here, we are taking the title and author fields of the request body, doing a bit of error handling, saving a new Book in our database, and finally returning it to the user.
To test, this you can use an HTTP client such as Postman or Insomnia
What happens when we try to create a new book?
Listening on port: 5000
(node:17884) UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property `title` of 'undefined' or 'null'.
at router.post (/home/ashwin/Projects/NodeJSWorkshop/src/routes/books.js:11:32)
at Layer.handle [as handle_request] (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/layer.js:95:5)
at next (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/layer.js:95:5)
at /home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:335:12)
at next (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:174:3)
at router (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:317:13)
at /home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:335:12)
at next (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:275:10)
at expressInit (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/middleware/init.js:40:5)
at Layer.handle [as handle_request] (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:317:13)
at /home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:335:12)
at next (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/router/index.js:275:10)
at query (/home/ashwin/Projects/NodeJSWorkshop/node_modules/express/lib/middleware/query.js:45:5)
(node:17884) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
AHH!!! What just happened? From line 2 it seems that req.body is null or undefined. The reason for that is because we are not actually parsing the request body. Express.js, and the entire Node.js ecosystem as a whole, takes the "opt-in" model, where you only add things to your app when you need them, like parsing HTTP request bodies. To solve this, add these 2 lines:
src/index.js
...
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api/books', books);
...
This tells express that we want to parse the HTTP request body for JSON and URL encoded bodies.
Last updated
Was this helpful?