与URL重写ExpressJS(URL Rewriting with ExpressJS)

2019-09-03 13:51发布

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

不过,我可能犯了一个错误?

我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)

Answer 1:

因此,我不得不排序相同的问题。 我写的使用历史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)不被改写或重新定向,而是在index.html文件将被送达并随后完成了所有的JS路由魔术。

希望这是接近你在找什么?



Answer 2:

你得到你想要使用的处理程序之前,您可以重写URL。

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

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

我已经使用了类似的方法做URL重写用正则表达式。



Answer 3:

如果没有有效的解决方案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那么中间件将服务于文件的照顾。



Answer 4:

你可以用if条件检查网址,并使用app.redirect重定向到一个特定的URL。



Answer 5:

试试这个:

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


文章来源: URL Rewriting with ExpressJS