Author:
** Backend Routing**⚓︎
Definiton⚓︎
When? (background)
client requests to a particular endpoint [ <=> URI + HTTP request method (GET, POST, ...) ].
What? (import routing)
To handle the request, in backend routing, the server handles every request by responding to the client with a code 201(I got that thing you want!) or a 404(Nope, I don’t have that!).
Situation? (example)
Let’s say a user fills out a form with his name, address, favorite hobby and sends a POST request by submitting the form. That GET request url is sent to the server, and the server serves back the requested url to the client as a static file that is stored on the server.
If the server doesn’t have the requested file, then it returns a 404 response error.
Usage⚓︎
Code using
Express.js syntax
app.METHOD(PATH, HANDLER)app: an instance of express.METHOD: an HTTP request method, in lowercase.PATH: a path on the server.HANDLER: the function executed when the route is matched.
Example code
//Respond with Hello World! on the homepage:
app.get('/', (req, res) => {
res.send('Hello World!')
})
//Respond to POST request on the root route (/), the application’s home page:
app.post('/', (req, res) => {
res.send('Got a POST request')
})
//Respond to a PUT request to the /user route:
app.put('/user', (req, res) => {
res.send('Got a PUT request at /user')
})
//Respond to a DELETE request to the /user route:
app.delete('/user', (req, res) => {
res.send('Got a DELETE request at /user')
})
Handlers for Routing⚓︎
Open the boxes below to see the examples 👇
Four classes of handler
1° A single callback function can handle a route
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello from A!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
2° More than one callback function can handle a route (use next() to control the next callback.)
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res, next) => {
console.log('the response will be sent by the next function ...')
next()
}, (req, res) => {
res.send('Hello from B!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
3° An array of callback functions can handle a route
const express = require('express')
const app = express()
const port = 3000
const cb0 = function (req, res, next) {
console.log('CB0')
next()
}
const cb1 = function (req, res, next) {
console.log('CB1')
next()
}
const cb2 = function (req, res) {
res.send('Hello from C!')
}
app.get('/', [cb0, cb1, cb2])
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
4° A combination of independent functions and arrays of functions can handle a route
const express = require('express')
const app = express()
const port = 3000
const cb0 = function (req, res, next) {
console.log('CB0')
next()
}
const cb1 = function (req, res, next) {
console.log('CB1')
next()
}
app.get('/', [cb0, cb1], (req, res, next) => {
console.log('the response will be sent by the next function ...')
next()
}, (req, res) => {
res.send('Hello from D!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})