[英]Express restart matching route - Redirect
我想在處理請求期間更改URL地址。 實現簡單的重定向。 如何更改當前URL並重新啟動Express中路由匹配的處理?
例如,我想要這樣的東西:
app.get "/r/*", (req, res, next) ->
changeURL = "/myverylongurl/param1....."
next(...)
以及代碼處理程序中某處的路線/ myverylongurl / ....
app.get "/myverylongurl/*", (req, res, next) ->
# complex thing here
因此,當有人請求http://myserverfake111.com/r/param1時 ,系統實際上會結束處理塊“此處有#個復雜的事物”。
使用req.session
可以跟蹤跨重定向的對象。 您的問題尚不清楚重定向的作用,但是如果您不需要保存任何內容,則可以跳過req.session並res.redirect("/myverylongurl/param1.....")
請求內部執行res.redirect("/myverylongurl/param1.....")
/r/*
處理函數。
var http = require('http')
var express = require('express');
var app = module.exports = express();
app.use(express.logger());
app.use(express.cookieParser('asdf 0a98234ja af anekjoavzcx'));
app.use(express.session());
app.use(app.router);
app.get('/'
,function(req, res){
res.send(200, 'Hi, /')
});
app.get("/r/*"
,function(req, res, next){
//res.redirect('/myverylongurl?param1=true');
req.session.changeURL = "/myverylongurl?param1=true";
next();
},function(req, res, next){
res.redirect('/foobar');
});
app.get('/foobar', function(req, res){
var destination = req.session.changeURL || "/";
if(req.session.changeURL){ delete req.session.changeURL };
res.redirect(destination);
});
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
http.createServer(app).listen(3000, function(){
console.log('Express server listening on port 3000');
});
聽起來好像您正在尋找使用相同代碼處理兩條路線。 如果您不想將301重定向與res.redirect
一起使用,請考慮對代碼進行組織,以便分離共享邏輯並為兩條路由執行該邏輯。 您仍然可以使用req.session
來維護內部處理程序功能鏈中的變量范圍,即使它不發送重定向也是如此。 或者,您可以通過回調將復雜的代碼置於其自己的函數中,並按需執行。
var http = require('http')
var express = require('express');
var app = module.exports = express();
app.use(express.logger('dev'));
app.use(express.cookieParser('asdf 0a98234ja af anekjoavzcx'));
app.use(express.session());
app.use(app.router);
app.get('/' ,function(req, res){ res.send(200, 'Hi, /') });
// strategy 1
function complexThings1(req, res, next){
console.log('#complex stuff started')
process.nextTick(function(){ // example asynchronous function
req.session.complexOutput="2i+3";
console.log('#complex stuff finished')
next();
});
};
app.get('/myverylongurl'
,complexThings1
,function(req, res){
res.send(200, 'complex things 1 output: '+req.session.complexOutput);
});
app.get("/r/*"
,complexThings1
,function(req, res){
res.send(200, 'complex things 1 output: '+req.session.complexOutput);
});
// strategy 2
function complexThings2(input, callback){
console.log('#complex stuff finished');
process.nextTick(function(){ // example asynchronous function
console.log('#complex stuff finished');
var output = input
callback(null, output);
});
};
app.get('/s/*', function(req, res){
complexThings2('8i+3.14', function(err, output){
res.send(200, 'complex things 2 output: '+output);
});
});
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
http.createServer(app).listen(3000, function(){
console.log('Express server listening on port 3000');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.