• 10 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/28/21

Create a POST route

Log in or subscribe for free to enjoy all this course has to offer!

Receive stuff from the front-end app

While we may not be able to store any data sent by the user for now, as we do not yet have a database set up, we can at least make sure that we are receiving data from the front end correctly.  The front end app has a "Sell a thing" form, which sends a POST request, containing the thing for sale, to our  api/stuff  endpoint.  Let's see how we can capture that data.

To handle the POST request coming from the front-end app, we'll need to be able to extract the JSON object from the request — we will need the  body-parser  package.  Install it as a production dependency using  npm  :

npm install --save body-parser

Import it in your  app.js  file:

const bodyParser = require('body-parser');

And set its  json  function as global middleware for your app, just after setting the response headers:

app.use(bodyParser.json());

Now that  body-parser  has parsed the request body, we can write the following POST middleware:

app.post('/api/stuff', (req, res, next) => {
console.log(req.body);
res.status(201).json({
message: 'Thing created successfully!'
});
});

Now, if you fill in the form in the front-end app and submit it, you should see the object you just created logged to your Node console!

Let's recap!

In this first part of the course:

  • you set up your development environment, with all of the dependencies you need to get started;

  • you spun up your first Node server, and used it to serve your first Express app

  • you created two routes for your app, and you implemented CORS to make sure the front end could safely make calls to your app.

In the next part of the course, you will add the essential MongoDB database layer to your app, in order to render it fully dynamic.

Example of certificate of achievement
Example of certificate of achievement