express request url code example

Example 1: express get full url

var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;

Example 2: express get raw path

const url = require('url');
const express = require('express');

const app = express();
app.use((req, res, next) => {
  const path = url.parse(req.url).path;
  // Do something...
});

const port = 3000;
app.listen(port, console.log(`Listening on port ${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: node js http request express

npm install [email protected]

Example 5: express render

// send the rendered view to the client
res.render('index')

// if a callback is specified, the rendered HTML string has to be sent explicitly
res.render('index', function (err, html) {
  res.send(html)
})

// pass a local variable to the view
res.render('user', { name: 'Tobi' }, function (err, html) {
  // ...
})

Tags:

Html Example