首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用ExpressJS重写URL

使用ExpressJS重写URL
EN

Stack Overflow用户
提问于 2013-05-16 13:11:35
回答 6查看 47K关注 0票数 30

我想重写我在ExpressJS网站上的URL。我用过这个插件,https://github.com/joehewitt/express-rewrite,但它不工作...

然而,我可能犯了一个错误...

我的app.js文件:

代码语言:javascript
复制
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'))
        })
    }
});

我的错误消息:

代码语言:javascript
复制
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)
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-06-22 15:18:42

所以我也遇到了同样的问题。我写了一个在浏览器上使用历史API的应用程序,我想把所有的非静态URL重写回index.html。所以对于静态文件,我这样做了:

代码语言:javascript
复制
app.configure(function() {
  app.use('/', express.static(__dirname + '/'));
});

但是对于历史API生成的路径,我这样做了:

代码语言:javascript
复制
app.get('*', function(request, response, next) {
  response.sendfile(__dirname + '/index.html');
});

这意味着不会重写或重定向任何不是/中的文件或目录的请求(例如历史API生成的URL ),而是为index.html文件提供服务,然后执行所有JS路由魔术。

希望这接近你正在寻找的东西?

票数 42
EN

Stack Overflow用户

发布于 2014-01-08 03:18:02

您可以在到达要使用的处理程序之前重写URL。

代码语言:javascript
复制
app.use(function(req, res, next) {
   if (req.url === '/toto') {
     req.url = '/heytoto';
   }
   next();
});

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

我使用了一个similar method来用正则表达式重写网址。

票数 44
EN

Stack Overflow用户

发布于 2014-04-10 00:14:58

没有response.sendfile(..)的解决方案是使用在app.use(express.static(..))之前插入的重写中间件,如下所示:

代码语言:javascript
复制
// 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中间件将负责为文件提供服务。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16579404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档