Skip to content

Instantly share code, notes, and snippets.

@oroce
Last active June 16, 2021 21:05
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save oroce/7fcca9b309fa0a393538 to your computer and use it in GitHub Desktop.
Save oroce/7fcca9b309fa0a393538 to your computer and use it in GitHub Desktop.
Redirect express while keeping params
/*
URLs:
http://localhost:3000/arrive?foo=bar&utm_source=smart-redir
*/
var redirect = require('redirect-with-params');
var express = require('express');
var app = express();
app.use(redirect({
params: ['foo'],
utm: true,
key: 'smartRedirect'
}));
app.get('/arrive', function(req, res) {
res.smartRedirect(302, '/');
});
app.get('/', function() {
assert(req.query.foo, 'bar');
assert(req.query.utm_source, 'smart-redir');
res.end('beep');
});
app.listen(3000);
var url = require('url');
function redirect(query, redir, options) {
var uri = url.parse(options.url, true);
delete uri.href;
delete uri.search;
uri.query = options.params.reduce(function(obj, param) {
var val = query[param];
if (val) {
obj[param] = val;
}
return obj;
}, uri.query || {});
var uriWithParams = url.format(uri);
redir(status, uriWithParams);
}
module.exports = function(options) {
options = options || {};
var key = options.key || 'redirectWithParams';
var utm = !!options.utm;
var params = [];
if (key === 'redirect') {
throw new Error('You cannot override res.redirect');
}
if (options.params) {
params.push.apply(params, options.params);
}
if (utm) {
params.push(
'utm_source',
'utm_medium',
'utm_campaign',
'utm_content',
'utm_term'
);
}
return function middleware(req, res, next) {
res[key] = function(url) {
var address = url;
var body;
var status = 302;
// allow status / url
if (arguments.length === 2) {
if (typeof arguments[0] === 'number') {
status = arguments[0];
address = arguments[1];
} else {
status = arguments[1];
}
}
redirect(query, res.redirect.bind(res), {
url: address,
status: status,
params: params
});
};
};
};
module.exports.redirect = redirect;
/*
URLs:
http://localhost:3000/arrive?foo=bar&utm_source=smart-redir
*/
var redirect = require('redirect-with-params').redirect;
var express = require('express');
var app = express();
app.use(redirect({
params: ['foo'],
utm: true,
key: 'smartRedirect'
}));
app.get('/arrive', function(req, res) {
redirect(req.query, res.redirect.bind(res), {
params: [
'foo',
'utm_source',
'utm_medium',
'utm_campaign',
'utm_content',
'utm_term'
],
url: '/',
status: 302,
key: 'smartRedirect'
});
});
app.get('/', function() {
assert(req.query.foo, 'bar');
assert(req.query.utm_source, 'smart-redir');
res.end('beep');
});
app.listen(3000);
@barnabas21
Copy link

Hello!
Please i want to know how i can do the same thing but with multiple files
For example i want to have a "index.js" file in which i will use app.use() to call my routes with their parameters from another file "routes.js".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment