2017-10-05 63 views
-1

我試圖將我的應用程序與我的所有端點組織在一個文件中,並讓這些端點在另一個文件中調用或執行代碼。當我到達/路由時,getindexPage函數似乎根本不會被調用。我如何讓程序執行home.js文件中的代碼?謝謝。在多個文件中拆分代碼後不調用函數

routes.js

var home = require('../routes/home.js'); 
module.exports = function (app) { 

    app.get('/', function (req, res) { 
     //no functional code 
     home.getIndexPage(); //Function call does not seem to work. 

    }); 
}; 

home.js

var ejs = require('ejs'); 

function getIndexPage (req, res) { 
    res.render('index.ejs'); 
    res.console.log('got to function'); 
    //No console message, no error. 
}; 

module.exports.getIndexPage = function getIndexPage() { }; 
+2

這個帖子裏絕對沒有問題...... – Salketer

+0

你確定res.console.log? – Salketer

回答

1

有兩個問題。首先,您不會將req和res傳遞給您的函數調用。其次,你正在導出一個空的函數。

//routes.js 
app.get('/', home.getIndexPage); 

//home.js 
module.exports.getIndexPage = getIndexPage; 
+0

謝謝!!!!!如果我想在主頁上運行兩個或更多功能,該怎麼辦? – RMichalowski

+0

好吧,既然你不想在你的路由中使用任何函數代碼,就可以在getIndexPage函數中調用它們。 – Salketer