📝 Edit page
➕ Add page
API
Handle query parameters
Example based on Getting started with NodeJS for frontend developers - Part 2.
Add the following to base example, before .listen
.
Note use of .query
.
index.js
app.get('/foo', (req, resp) => { const { name } = req.query; resp.send(`Hello, ${name || world}!`); })
Test on:
Handle URL parameters
Add the following to base example, before .listen
.
Note use of .params
.
app.get('/foo/:name', (req, resp) => {
const { name } = req.params;
resp.send(`Hello, ${name || world}!`);
})
Test on:
Handle POST request
index.js
app.use(express.json()); app.use(express.urlencoded()); app.post('/foo', (req, res) => { res.status(201) .send({ message: "Created a foo using data", data: req.body, }); });
Handle other status codes
index.js
app.get('/admin', (_req, res) => { res.sendStatus(401); });