2013-05-16 50 views
18

我想在我的ExpressJS網站上重寫我的URL。我用這個插件,https://github.com/joehewitt/express-rewrite,但它不工作...用ExpressJS URL重寫

不過,我可能犯了一個錯誤......

我app.js文件:

var express = require('express') 
    , index = require('./routes/index.js') 
    , admin = require('./routes/admin.js') 
    , contact = require('./routes/contact.js') 
    , posts = require('./routes/posts.js') 
    , http = require('http') 
    , path = require('path') 
    , hash = require('./auth').hash 
    , db = require('./models') 
    , favicons = require('connect-favicons') 
    , rewriter = require('express-rewrite'); 


var app = express(); 

app.configure(function() { 
    app.set('port', process.env.PORT || 3000); 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.favicon(__dirname + '/public/images/FAVICON.ico')); 
    app.use(favicons(__dirname + '/public/images/apple-touch-icon.png')); 
    app.use(express.logger('dev')); 
    app.use(express.bodyParser()); 
    app.use(express.static(path.join(__dirname, 'public'))); 
    app.use(express.cookieParser()); 
    app.use(express.cookieSession({ 
      secret: 'SECRET', 
      cookie: { access: false } 
     }) 
    ); 
    app.use(rewriter); 
    app.use(app.router); 
    app.use(function(req, res, next){ 
     res.render('404.jade', { 
      title: "404 - Page Not Found", 
      showFullNav: false, 
      status: 404, 
      url: req.url 
     }); 
    }); 
}); 

app.configure('development', function() { 
    app.use(express.errorHandler()); 
}); 

app.get('/', index.index); 

app.get('/toto', rewriter.rewrite('/heytoto')); 

db.sequelize.sync().complete(function(err) { 
    if (err) { 
     throw err 
    } else { 
     http.createServer(app).listen(app.get('port'), function(){ 
      console.log('Express server listening on port ' + app.get('port')) 
     }) 
    } 
}); 

我的錯誤信息:

Express 
500 TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'match' 
at Object.rewriter [as handle] (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express-rewrite/rewrite.js:3:26) 
at next (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express/node_modules/connect/lib/proto.js:199:15) 
at Object.cookieSession [as handle] (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express/node_modules/connect/lib/middleware/cookieSession.js:113:5) 
at next (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express/node_modules/connect/lib/proto.js:199:15) 
at Object.cookieParser [as handle] (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5) 
at next (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express/node_modules/connect/lib/proto.js:199:15) 
at resume (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express/node_modules/connect/lib/middleware/static.js:60:7) 
at SendStream.error (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express/node_modules/connect/lib/middleware/static.js:73:37) 
at SendStream.EventEmitter.emit (events.js:126:20) 
at SendStream.error (/Users/anthonycluse/Sites/Anthony-Cluse-Express/node_modules/express/node_modules/send/lib/send.js:147:51) 

回答

28

所以我有類似的問題。我編寫了一個在瀏覽器上使用歷史API的應用程序,我想將所有非靜態URL重寫爲index.html。因此,對於靜態文件我所做的:

app.configure(function() { 
    app.use('/', express.static(__dirname + '/')); 
}); 

但後來的歷史API生成的路徑我所做的:

app.get('*', function(request, response, next) { 
    response.sendfile(__dirname + '/index.html'); 
}); 

這意味着,這不是在/一個文件或目錄的任何請求(例如:由歷史API生成的URL)不會被重寫或重定向,而是將被服務並且然後執行所有JS路由魔術。

希望這接近你要找的東西?

+0

是的,這是我在找的東西,謝謝! – tonymx227

+0

這也適用於我!謝謝! – wmock

+0

我無法得到它的工作,可以幫助我嗎? http://stackoverflow.com/questions/27971489/refresh-specific-page-with-angular-express-and-jade-using-html5mode?noredirect=1#comment44494193_27971489 –

-2

試試這個:

app.get('/toto', function(req, res) { 
    res.redirect('/heytoto'); 
}); 
+3

是的,但它是一個重定向...我想重寫我的網址我不想使用「res.redirect()」功能。 – tonymx227

16

您可以在到達要使用的處理程序之前重寫URL。

app.use(function(req, res, next) { 
    if (req.url === '/toto') { 
    req.url = '/heytoto'; 
    } 
    next(); 
}); 

app.get('/heytoto', ...); 

我用similar method來做正則表達式的URL重寫。

2

,沒有response.sendfile(..)有效的解決方案是爲使用像這樣app.use(express.static(..))前插入一個重寫中間件:

// forward all requests to /s/* to /index.html 

app.use(function(req, res, next) { 

    if (/\/s\/[^\/]+/.test(req.url)) { 
    req.url = '/index.html'; 
    } 

    next(); 
}); 

// insert express.static(...) 

這樣,expressjs正確識別重寫。然後static中間件將負責提供文件。