预加载/预先需要.js路由文件的目录

使用Express与Node.js,我们可能会这样做:

app.use('api/:controller/:action/:id', function(req,res,next){ var controller = req.params.controller; var action = req.params.action; var route = require('./routes/' + controller + '/' + action); route(req,res,next); } 

现在这一切都很好,除了至less有一个问题:如果这个文件还没有“需要”,路由文件在运行时dynamic加载。 这意味着它至less有点慢。

有人有一个通过目录recursion的脚本,并在服务器首次启动时预先加载/预先要求所有.js文件?

我也有类似的问题,使用RequireJS的前端。 该解决scheme似乎是编写一个bash脚本,将目录及其子目录中的所有.js文件path写入文本文件。 那么当服务器启动时,它会读取该文本文件,并且需要在文本文件中列出的目录中的所有文件。 这是做这件事的最好方法吗?

如果可以使用io.js ,则可以使用命令行-r--require预加载模块:

 iojs -r <module_name> server.js 

我创build了一个NPM模块,为前端做这个,做Node.js / CommonJS是另一回事。

https://www.npmjs.com/package/requirejs-metagen

你可以像这样使用它:

 var grm = require('requirejs-metagen'); //you can use with Gulp var controllersOpts = { inputFolder: './public/static/app/js/controllers/all', appendThisToDependencies: 'app/js/controllers/', appendThisToReturnedItems: '', eliminateSharedFolder: true, output: './public/static/app/js/meta/allControllers.js' }; grm(controllersOpts,function(err){ //handle errors your own way }); 

它会生成相应的AMD / RequireJS模块,如下所示:

 define( [ "app/js/controllers/all/jobs", "app/js/controllers/all/users" ], function(){ return { "jobs": arguments[0], "users": arguments[1] } }); 

你也可以要求子目录和所有这样的东西:

  var allViewsOpts = { inputFolder: './public/static/app/js/jsx', appendThisToDependencies: 'app/js/', appendThisToReturnedItems: '', eliminateSharedFolder: true, output: './public/static/app/js/meta/allViews.js' } grm(allViewsOpts ); 

这样产生的输出如下所示:

 define([ "app/js/jsx/BaseView", "app/js/jsx/reactComponents/FluxCart", "app/js/jsx/reactComponents/FluxCartApp", "app/js/jsx/reactComponents/FluxProduct", "app/js/jsx/reactComponents/Item", "app/js/jsx/reactComponents/Job", "app/js/jsx/reactComponents/JobsList", "app/js/jsx/reactComponents/listView", "app/js/jsx/reactComponents/Picture", "app/js/jsx/reactComponents/PictureList", "app/js/jsx/reactComponents/RealTimeSearchView", "app/js/jsx/reactComponents/Service", "app/js/jsx/reactComponents/ServiceChooser", "app/js/jsx/reactComponents/todoList", "app/js/jsx/relViews/getAll/getAll", "app/js/jsx/relViews/jobs/jobsView", "app/js/jsx/standardViews/dashboardView", "app/js/jsx/standardViews/overviewView", "app/js/jsx/standardViews/pictureView", "app/js/jsx/standardViews/portalView", "app/js/jsx/standardViews/registeredUsersView", "app/js/jsx/standardViews/userProfileView" ], function(){ return { "BaseView": arguments[0], "reactComponents/FluxCart": arguments[1], "reactComponents/FluxCartApp": arguments[2], "reactComponents/FluxProduct": arguments[3], "reactComponents/Item": arguments[4], "reactComponents/Job": arguments[5], "reactComponents/JobsList": arguments[6], "reactComponents/listView": arguments[7], "reactComponents/Picture": arguments[9], "reactComponents/PictureList": arguments[10], "reactComponents/RealTimeSearchView": arguments[11], "reactComponents/Service": arguments[12], "reactComponents/ServiceChooser": arguments[13], "relViews/getAll/getAll": arguments[14], "relViews/jobs/jobsView": arguments[15], "standardViews/dashboardView": arguments[16], "standardViews/overviewView": arguments[17], "standardViews/pictureView": arguments[18], "standardViews/portalView": arguments[19], "standardViews/registeredUsersView": arguments[20], "standardViews/userProfileView": arguments[21] } }); 

我需要更新库,所以它返回的stream,所以你可以处理,当它完成,否则它的效果很好。