2013-06-19 41 views
14

Có thể tạo mã như thế này trong node.js không?nodejs tương đương với điều này .htaccess

<IfModule mod_rewrite.c> 
     RewriteEngine on 

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

</IfModule> 

hiển thị url tuyến không phải là "chế độ xem" và cũng không tồn tại tệp sau đó viết index.html.

sử dụng một cái gì đó giống như express hoặc connect

UPDATE: Tôi cần một biểu thức chính quy cho !/(view)/ trong lộ trình cho express trong node.js.

+0

htaccess biết rằng tuyến đường là chế độ xem? Nó có kiểm tra xem nó có kết thúc bằng .html không? – verybadalloc

+1

Tôi hiểu rằng "chế độ xem" là tên thư mục, yêu cầu uri áp dụng chuỗi biểu thức chính quy trong url – Glats

Trả lời

15

Các bạn đã thử:

  1. Phục vụ tĩnh
  2. Catch/view URL
  3. Catch mọi thứ khác

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

HOẶC

  1. Phục vụ tĩnh
  2. Kiểm tra xem URL là/xem

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

HOẶC

  1. tĩnh Catch như thường lệ
  2. Catch KHÔNG/xem

    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

Cảm ơn bạn đã cung cấp nhiều giải pháp. Giải pháp thứ hai chỉ là giải pháp tôi đang tìm kiếm! –

+2

ai đó có thể giải thích những gì đang xảy ra ở đây? – Martian2049

2

Các cuối cùng cấu trúc là:

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

nhờ tất cả mọi người. PD: hiển thị html với mô-đun ejs

+0

Cảm ơn bạn, cảm ơn bạn rất nhiều! ... sau khi một số tinkering, nó làm việc cho tôi quá, KHÔNG NGAY qua tâm trí của tôi để tìm kiếm một gói phân tích các mẫu html2js nhưng tôi biết một cái gì đó đã được thực hiện về điều đó! –

Các vấn đề liên quan