sabbir
sabbir

Reputation: 2025

node.js - redirect to another url in middleware

In my MEAN SPA application, I want to redirect all url request to index with additional parameters. For example, when user type http://localhost:3003/, it should call index page. When user type http://localhost:3003/game/playstation/ps4, it also should call index page, with extra parameters like game, palystation, ps4, but It redirect to index without extra parameters. My Code is given below:

 app.use(function(req, res, next) {
    // get the url without main domain address
    var original_url =  req.originalUrl;

    // convert string url to array
    var url_part = original_url.split('/');

    // remove first array, bcz first is always ''
    url_part.shift();

    // check if url is main url request without any parameter
    if( url_part[0] !== '' ) {
        // if not, then store all url_part array
        req.request_url = url_part;

        // redirct to main '/' call
        // when it redirect to main url, then req.request is empty
        res.redirect('/');
     }

    next();
});

app.get('/', function( req, res) {
     // here req.request_url is always 'undefined'
     console.log("req.request_url : ", req.request_url);

      return res.render('index.ejs', {
         url: req.request_url
      });
});

EDIT

I have to redirect to index route not any others, because in my SPA application, I just load my index route with default js, css and others, and all others will be call from api.

Thanks in advance.

Upvotes: 1

Views: 2107

Answers (3)

Klante
Klante

Reputation: 141

You could do it like this and both will map to the same function but will have the parameters set or null if it maps from the '/' route

var indexPageFunc = function(res, req){
  var type = req.param('type'); // game or null
  var system = req.param('system'); // playstation or null
  var version = req.param('version'); // ps4 or null
  res.render('index.ejs');
}
app.get('/', indexPageFunc);
app.get('/:type/:system/:version, indexPageFunc);

Upvotes: 1

DrakaSAN
DrakaSAN

Reputation: 7863

You have two options: redirect to another URL and include GET parameters.

var express = require('express'),
    app = express();

app.use(function(req, res, next) {
    var url_part = req.originalUrl.split('/');
    console.log('' + req.originalUrl + ' => ' + url_part);
    url_part.shift();
    if( url_part[0] !== '' ) {
        //You could add as GET parameters, but it will be shown to the user
        res.redirect('/?request_url=' + url_part);
     }

    next();
});

app.get('/', function (req, res) {
     // here req.request_url is always 'undefined'
     console.log("req.request_url : ", req.request_url);

      return res.json({
         url: req.request_url
      });
});

app.listen(8080);

Or you'll have to find a way to rewrite the URL, and do a POST request.

Upvotes: 0

Alexey B.
Alexey B.

Reputation: 12033

You cant do it in that way. Redirect send 302 to browser, and user make new separate request to your server.

You can just return same response for all routes

app.get('*', function(req, res) {
    // get the url without main domain address
    var original_url =  req.originalUrl;

    // convert string url to array
    var url_part = original_url.split('/');

    // remove first array, bcz first is always ''
    url_part.shift();

    res.render('index.ejs', {
        url: url_part
    });
});

Upvotes: 0

Related Questions