18

でこのようなコードを作成することは可能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 が「view」ではなく、ファイルが存在しない場合は write index.html.

expressまたはのようなものを使用してconnect

更新: in の in ルートの正規表現が必要!/(view)/です。expressnode.js

4

3 に答える 3

20

やってみました:

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

また

  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');
    });
    
于 2013-06-21T18:53:31.720 に答える
6

最終的な構造は次のとおりです。

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 をレンダリングする

于 2013-06-28T17:43:00.380 に答える