(Re)defining routes

Next, let's migrate our requestHandler function to Express. With Express, instead of defining a single request handler for all our routes, we can define request handlers for each route using the format app.METHOD('path', callback), where METHOD is the HTTP method of the request.

Therefore, replace our previous requestHandler function with an app.post call. This is our old implementation:

function requestHandler(req, res) {  if (req.method === 'POST' && req.url === '/users') {    // Handler logic for POST /user  } else {    res.writeHead(200, { 'Content-Type': 'text/plain' });    res.end('Hello, World!');  }}

And this is our new implementation:

app.post('/users', (req, res) => {  // Handler logic for POST /user});

The req and res 

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.