September 2018
Intermediate to advanced
764 pages
20h 6m
English
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
Read now
Unlock full access