如何将参数传递到外部模块中
How to pass arguments into an outside module?
我有以下函数,我把它放在一个单独的js文件中。
我正在尝试在另一个需要护照.js的 javascript 文件中使用它,我想使用 app.use
来调用它以进一步模块化我的应用程序
var express = require('express');
var router = express.Router();
/* GET welcome page. */
router.get('/home', isLoggedIn, function(req, res, next) {
res.render('home', {
title: 'title',
user : req.user
});
});
// route middleware to make sure
function isLoggedIn(req, res, next) {
// if user is authenticated in the session
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}
module.exports = router;
我创建它的原因是减少冗余并且每次都不使用以下代码:
app.get('/home', isLoggedIn, function(req, res) {
res.render('home.ejs', {
user : req.user // get the user out of session and pass to template
});
});
但是我似乎无法让它工作。 经过身份验证是未定义的,尽管它位于同一文件夹中,并且在我发出 get 时给我一个错误 404 找不到。我如何将其保存在外部文件中并仍然使用它?我是否也应该从我调用它的地方传递user
参数?
var express = require('express');
var router = express.Router();
module.exports = function (app, passport){
router.get('/home', isLoggedIn, function(req, res, next) {
res.render('home', {
title: 'title',
user : req.user
});
});
return router;
};
function isLoggedIn(req, res, next) {
// if user is logged in -
if (req.isAuthenticated())
return next();
// if they aren't redirect them to home
res.redirect('/');
}
相关文章:
- 使用外部模块继承Jade模板的好策略,在Express上
- 如何在RT上添加插件和使用一些外部模块/文件
- 如何将参数传递到外部模块中
- AngularJS参考工厂.来自外部模块的功能
- 在 TypeScript 中使用外部模块声明一个全局变量
- 异步功能取决于量角器测试中的外部模块
- Casperjs赢得'不允许使用外部模块
- Node.JS+Socket.io,将套接字事件处理程序移动到外部模块
- 如何用oclazyload加载的外部模块加载Angular translate
- Angular+TypeScript+外部模块.如何跨文件维护类型信息
- 使用angular js中的外部模块服务解析状态
- 类已经或正在使用名称'SafeUrl'从外部模块获取,但不能命名
- Angular Animation在路由到外部模块时出错[Cannot read property 'play&
- 击倒内部/外部模块!==短暂/单身的生活方式
- Webpack外部模块名称带连字符
- 是否可以将多个内部模块组合到一个外部模块?
- 要求/AMD外部模块分辨率
- Angular.js -不能注入外部模块
- 当类型声明为环境外部模块定义时,如何声明参数类型
- 从外部模块注入提供程序到配置块时出现未知的提供程序错误