2013-06-19 14 views
14

Czy jest możliwe zbudowanie takiego kodu w node.js?odpowiednik nodejs tego .htaccess

<IfModule mod_rewrite.c> 
     RewriteEngine on 

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

</IfModule> 

url wyświetlanie trasy nie jest „Widok”, a także plik nie istnieje, a następnie napisać index.html.

użyciu coś jak express lub connect

UPDATE: Potrzebuję wyrażenie regularne dla !/(view)/ w trasie dla express w node.js.

+0

W jaki sposób htaccess wie, że trasa jest widokiem? Czy sprawdza, czy kończy się na .html? – verybadalloc

+1

Rozumiem, że "widok" jest nazwą katalogu, zapytanie uri stosuje ciąg regularnego wyrażenia w url – Glats

Odpowiedz

15

Czy próbowałeś:

  1. Podawać statyki
  2. Catch/zobaczyć URL
  3. Złap wszystko

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

LUB

  1. Podawać statyki
  2. Sprawdź, czy adres URL jest/Widok

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

LUB

  1. statyka złapać jak zwykle
  2. połów nie/widok

    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

Dzięki za oferowanie wielu rozwiązań. Drugie rozwiązanie jest właśnie tym, którego szukałem! –

+2

Czy ktoś może wyjaśnić, co się tutaj dzieje? – Martian2049

2

Końcowy struktura jest następująca:

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

dzięki wszystkim. PD: renderuj html z modułem ejs

+0

Dziękuję, dziękuję bardzo! ... po jakimś majsterkowaniu, działa to również dla mnie, NIE MASZ NAWET mylić, żeby wyszukać pakiet, który parsuje szablony html2js, ale wiedziałem, że trzeba coś z tym zrobić! –