nodejs 相当于这个 .htaccess

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17199095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:50:21  来源:igfitidea点击:

nodejs equivalent of this .htaccess

node.js.htaccess

提问by Glats

Is it possible to build a code like this in node.js?

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

<IfModule mod_rewrite.c>
???? RewriteEngine on

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

</IfModule>

url display a route is not "view" and also the file does not exist then write index.html.

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

using something like expressor connect

使用类似expressconnect

UPDATE: I need a regular expression for !/(view)/in route for expressin node.js.

更新:我需要一个正则表达式!/(view)/的路线expressnode.js

采纳答案by Glats

The final structure is:

最终的结构是:

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'));

Thanks everyone. PD: Render html with module ejs

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

回答by sbstjn

Have you tried:

你有没有尝试过:

  1. Serve statics
  2. Catch /view URL
  3. Catch everything else

    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
  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'); 
    });
    

OR

或者

  1. Serve statics
  2. Check if URL is /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. 检查 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');
        }
      });
    });
    

OR

或者

  1. Catch statics as usual
  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');
    });
    
  1. 像往常一样捕捉静态
  2. 赶不上/查看

    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');
    });
    

回答by Bojoer

I would recommend using the express Middleware urlrewrite.

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

If you don't handle rewrites on a reverse proxy for example and if using express and want regex support for flexibility use: https://www.npmjs.com/package/express-urlrewrite

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