tüm rota işlevlerini diğer dosyalara (modüller) koyabilir ve ana sunucu dosyasına bağlayabilirsiniz. ana ekspres dosyasında, modülü sunucuya bağlayacak bir işlev ekleyin:
function link_routes(app, route_collection){
route_collection['get'].forEach(route => app.get(route.path, route.func));
route_collection['post'].forEach(route => app.post(route.path, route.func));
route_collection['delete'].forEach(route => app.delete(route.path, route.func));
route_collection['put'].forEach(route => app.put(route.path, route.func));
}
ve her rota modeli için bu işlevi çağırın:
link_routes(app, require('./login.js'))
modül dosyalarında (örneğin - login.js dosyası), işlevleri her zamanki gibi tanımlayın:
const login_screen = (req, res) => {
res.sendFile(`${__dirname}/pages/login.html`);
};
const forgot_password = (req, res) => {
console.log('we will reset the password here')
}
ve bunu istek yöntemi ile anahtar olarak dışa aktarırsanız, değer her biri yol ve işlev tuşlarına sahip bir nesne dizisidir.
module.exports = {
get: [{path:'/',func:login_screen}, {...} ],
post: [{path:'/login:forgotPassword', func:forgot_password}]
};