【问题标题】:nodejs equivalent of this .htaccessnodejs 相当于这个 .htaccess
【发布时间】:2013-06-19 18:54:32
【问题描述】:

是否可以在node.js 中构建这样的代码?

<IfModule mod_rewrite.c>
     RewriteEngine on

     RewriteCond% {REQUEST_URI}! / (View) / [NC]
     RewriteCond% {REQUEST_FILENAME}!-F
     RewriteRule ^ (. *) $ Index.html [L, QSA]

</IfModule>

url 显示路由不是“视图”并且文件不存在然后写index.html

使用 expressconnect 之类的东西

更新:我需要一个用于!/(view)/ 的正则表达式,用于node.js 中的express

【问题讨论】:

  • htaccess 怎么知道路由是视图?它是否检查它是否以 .html 结尾?
  • 我知道“view”是目录名,uri请求在url中应用正则表达式字符串

标签: node.js .htaccess


【解决方案1】:

你试过了吗:

  1. 服务静态
  2. 捕捉/查看网址
  3. 抓住一切

    app.configure(function(){
      app.use(express.static(__dirname+'/public')); // Catch static files
      app.use(app.routes);
    });
    
    // Catch /view and do whatever you like
    app.all('/view', function(req, res) {
    
    });
    
    // Catch everything else and redirect to /index.html
    // Of course you could send the file's content with fs.readFile to avoid
    // using redirects
    app.all('*', function(req, res) { 
      res.redirect('/index.html'); 
    });
    

  1. 服务静态
  2. 检查 URL 是否为 /view

    app.configure(function(){
      app.use(express.static(__dirname+'/public')); // Catch static files
      app.use(function(req, res, next) {
        if (req.url == '/view') {
          next();
        } else {
          res.redirect('/index.html');
        }
      });
    });
    

  1. 照常捕捉静态数据
  2. Catch NOT /view

    app.configure(function(){
      app.use(express.static(__dirname+'/public')); // Catch static files
      app.use(app.routes);
    });
    
    app.get(/^(?!\/view$).*$/, function(req, res) {
      res.redirect('/index.html');
    });
    

【讨论】:

  • 有人能解释一下这里发生了什么吗?
【解决方案2】:

最终的结构是:

var express = require('express'), url = require('url');

var app = express();
app.use(function(req, res, next) {
    console.log('%s %s', req.method, req.url);
    next();
});
app.configure(function() {
    var pub_dir = __dirname + '/public';
    app.set('port', process.env.PORT || 8080);
    app.engine('.html', require('ejs').__express);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'html');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.static(pub_dir));
    app.use(app.router);
});
app.get('/*', function(req, res) {
    if (req.xhr) {
        var pathname = url.parse(req.url).pathname;
        res.sendfile('index.html', {root: __dirname + '/public' + pathname});
    } else {
        res.render('index');
    }
});

app.listen(app.get('port'));

谢谢大家。 PD:使用模块 ejs 渲染 html

【讨论】:

    【解决方案3】:

    我建议使用快速中间件 urlrewrite。

    例如,如果您不处理反向代理上的重写,并且如果使用 express 并希望正则表达式支持以灵活使用:https://www.npmjs.com/package/express-urlrewrite

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      相关资源
      最近更新 更多