req params express code example
Example 1: express js params
app.get('/path/:name', function(req, res) {
console.log(req.params.name);
});
app.get('/path', function(req, res) {
console.log(req.query['name']);
});
Example 2: express hello world
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
Example 3: express redirect
res.redirect('/foo/bar')
res.redirect('http://example.com')
res.redirect(301, 'http://example.com')
res.redirect('../login')
Example 4: expressjs query params
console.dir(req.query.q)
console.dir(req.query.order)
console.dir(req.query.shoe.color)
console.dir(req.query.shoe.type)
console.dir(req.query.color)
Example 5: express url redirect
const app = require('express')();
app.get('/from', (req, res) => {
res.redirect('/to');
});
app.get('/to', (req, res) => res.send('Hello, World!'));
Example 6: express render
res.render('index')
res.render('index', function (err, html) {
res.send(html)
})
res.render('user', { name: 'Tobi' }, function (err, html) {
})