2016-12-15 45 views

回答

0

我要告诉你,你怎么能解决的Node.js /快递这个使用serve-static模块。

1)。首先安装开发环境模块:npm i -D serve-static。 2)。在api/hooks目录内创建serve-static目录。 3)。在前面创建的serve-static目录中创建index.js文件。 4)。添加以下内容:

module.exports = function serveStatic (sails) { 

    let serveStaticHandler; 

    if ('production' !== sails.config.environment) { 
    // Only initializing the module in non-production environment. 
    const serveStatic = require('serve-static'); 
    var staticFilePath = sails.config.appPath + '/.tmp/public'; 
    serveStaticHandler = serveStatic(staticFilePath); 
    sails.log.info('Serving static files from: «%s»', staticFilePath); 
    } 

    // Adding middleware, make sure to enable it in your config. 
    sails.config.http.middleware.serveStatic = function (req, res, next) { 
    if (serveStaticHandler) { 
     serveStaticHandler.apply(serveStaticHandler, arguments); 
    } else { 
     next(); 
    } 
    }; 


    return {}; 

}; 

5)。编辑config/http.js文件并添加先前定义的中间件:

module.exports.http = { 
    middleware: { 
    order: [ 
     'serveStatic', 
     // ... 
    ] 
    } 
}; 

6)。重新启动/运行您的应用程序,例如node ./app.js并尝试获取一个静态文件。它应该工作。

相关问题