2013-06-19 11 views
14

È possibile creare un codice come questo in node.js?nodejs equivalente di questo .htaccess

<IfModule mod_rewrite.c> 
     RewriteEngine on 

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

</IfModule> 

URL di visualizzazione di un percorso non è "vista" e anche il file non esiste, allora scrivere index.html.

usando qualcosa come express o connect

UPDATE: Ho bisogno di un'espressione regolare per !/(view)/ in rotta per express in node.js.

+0

Come fa htaccess a sapere che una rotta è una vista? Verifica se termina con .html? – verybadalloc

+1

Capisco che "vista" sia il nome della directory, la richiesta uri si applica alla stringa di espressione regolare nell'URL – Glats

risposta

15

Hai provato:

  1. Servire statica
  2. Cattura/vista URL
  3. Cattura tutto il resto

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

O

  1. Servire statica
  2. Controllare se l'URL è/vista

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

O

  1. statica di cattura come al solito
  2. cattura non/vista

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

Grazie per aver offerto più soluzioni. La seconda soluzione è proprio quella che stavo cercando! –

+2

qualcuno può spiegare cosa sta succedendo qui? – Martian2049

2

Il finalmente struttura è:

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

grazie a tutti. PD: render html con modulo ejs

+0

Grazie, grazie mille! ... dopo alcuni aggiustamenti, funziona anche per me, non mi è mai venuto in mente di cercare un pacchetto che analizza i template html2js ma sapevo che qualcosa doveva essere fatto a riguardo! –