express js documentation code example
Example 1: express.js server
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("This is home page.");
});
app.post("/", (req, res) => {
res.send("This is home page with post request.");
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on PORT: ${PORT}`);
});
Example 2: express redirect
res.redirect('/foo/bar')
res.redirect('http://example.com')
res.redirect(301, 'http://example.com')
res.redirect('../login')
Example 3: http header express
app.get('/', (req, res) => {
req.header('User-Agent')
})
Example 4: what is express.js
Express is a minimal and flexible Node.js web application framework
that provides a robust set of features for web and mobile
applications.
Example 5: node js http request express
npm install request@2.81.0
Example 6: express render
res.render('index')
res.render('index', function (err, html) {
res.send(html)
})
res.render('user', { name: 'Tobi' }, function (err, html) {
})