使用 ExpressJS 重写 URL [英] URL Rewriting with ExpressJS

查看:38
本文介绍了使用 ExpressJS 重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to rewrite my URLs on my ExpressJS website. I've used this plugin, https://github.com/joehewitt/express-rewrite, but it doesn't work...

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

However, I might have made a mistake...

我的 app.js 文件:

My app.js file :

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)

推荐答案

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

So I had sort of the same issue. I wrote an app that uses the history API on browsers and I wanted to rewrite all non-static URLs back to index.html. So for static files I did:

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

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

But then for the history API generated paths I did:

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

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

This meant that any request that wasn't a file or directory in / (such as a URL generated by the history API) wouldn't be rewritten or redirected but instead the index.html file will be served and that then does all the JS routing magic.

希望这与您正在寻找的内容相近吗?

Hopefully that's close to what you're looking for?

这篇关于使用 ExpressJS 重写 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆