我见过很多人在他们的 routes/index.js 中动态生成他们的所有路由,如下所示:
require("fs").readdirSync("./routes", 'utf8').forEach(function(file) {
if (file != 'index.js' && file != '.DS_Store') {
require("./"+file);
}
});
这在开发中可以正常工作,但在生产中却不行。如果我删除它并手动添加路线,它工作正常。有任何想法吗?
如果您认为这会有所帮助,这是我的错误:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
Error: ENOENT, No such file or directory './routes'
at Object.readdirSync (fs.js:376:18)
at Object.<anonymous> (/app/routes/index.js:4:15)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at require (module.js:346:19)
at Object.<anonymous> (/app/server.js:50:14)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
Process died with exit code 1. Restarting...