我是Node的新手,我总共有三个JS文件:
Index.js包含以下代码:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
console.log(arguments);
if(req.isAuthenticated()){
res.render('index', {
title: 'sp1 - My Application',
user: req.user
});
}else{
console.log('not authentcated sending to authenticate');
res.redirect('/login');
}
});
module.exports = router;
SSO.js包含以下代码:
var express = require('express');
var router = express.Router();
router.get('/spinitsso-redirect', function (req, res) {
console.log('got a redirection from idp');
sp.sendLoginRequest(idp, 'redirect', function (url) {
res.redirect(url);
});
});
module.exports = router;
App.js包含以下代码:
var express = require('express');
var app = express();
var server = app.listen(4002, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
问题:
两个不同的文件(index.js和sso.js)如何能够将中间件添加到同一个路由器实例?这是Express的一些静态属性,您可以在代码中的任何JS文件中添加路由器/ middlerware吗?
编辑: 添加了缺少的module.exports = router;在index.js和SSO.js文件中。这行代码如何工作?
Link完成代码
答案 0 :(得分:0)
您的代码缺少module.exports = router;
var express = require('express');
var router = express.Router();
router.get('/', function (req, res, next) {
res.render('index');
});
module.exports = router;