node.js/express: Sending static files if they exist

前端 未结 2 1860
既然无缘
既然无缘 2021-01-13 17:17

I want to serve html files from a folder if they exist, otherwise fallback to the dynamic app.

currently I use something like:



        
2条回答
  •  無奈伤痛
    2021-01-13 18:02

    You are using a lot of boilerplate. It is easier using routes. Here is an example:

    var routes = require('./routes');
    
    app.configure(function () {
        ...
        app.use(express['static'](path.join(__dirname, '/../WebContent')));
        app.use(app.router);
    });
    
    // Routes
    app.get('/', routes.index);
    

    routes/index.js:

    exports.index = function (req, res) {
       res.render('index');
    };
    

    Rendering your webapp root outside your project root is very strange. Feels like bad practice. Is this necessary?

提交回复
热议问题