私はこの記事に沿ってフォローしています。この記事では、ルートをエクスプレスで整理するための良い方法について説明しています。main.jsファイルからエクスポートした関数にアクセスしようとすると問題が発生します。「localhost/user / username」をカールすると、404エラーが発生します
//the routes section of my my app.js file
app.get('/', routes.index);
app.get('/user/:username', routes.getUser);
//my index.js file
require('./main');
require('./users');
exports.index = function(req, res) {
res.render('index', {title: 'Express'});
};
//my main.js file
exports.getUser = function(req, res){
console.log('this is getUser');
res.end();
};
----私のソリューションで編集----
これが私が行った解決策です、多分誰かがそれが役に立つと思うでしょう。また、これが将来問題を引き起こすかどうかについての提案を聞くこともできます。
//-------The routes in my app.js file now look like this.
require('./routes/index')(app);
require('./routes/main')(app);
//-------In index.js i now have this
module.exports = function(app) {
app.get('/', function(req,res){
res.render('index', {title: 'Express'});
});
};
//-------My main.js now looks like this-------
module.exports = function(app){
app.get('/user/:username', function(req, res){
var crawlUser = require('../engine/crawlUser');
var username = req.params.username;
crawlUser(username);
res.end();
});
};