Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs equivalent of this .htaccess

Is it possible to build a code like this in 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.

using something like express or connect

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

like image 227
jics Avatar asked Jun 19 '13 18:06

jics


1 Answers

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

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

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');
    });
    
like image 123
sbstjn Avatar answered Sep 21 '22 07:09

sbstjn