我对node.js相当陌生,我开发了一个应用程序,要求我向用户发送邮件。我正在使用会话存储用户的“用户名”。因此,我编写了一个向用户发送邮件的控制器。下面是代码,
mailcontroller.js
var mongoose = require('mongoose');
var email = require('nodemailer');
var node = require('node-schedule');
mongoose.connect("URL",{ useUnifiedTopology: true, useNewUrlParser: true });
var pro = mongoose.model('signup');
var sendEmail = node.scheduleJob('*/60 * * * *', function(){
pro.find({username:"#I NEED SESSION OBJECT'S KEY HERE"},function(err,data){
//logic
}
};
下面是我的 app.js 文件
var mailcontroller = require('./controllers/mailcontroller');
var session = require('express-session');
var redis = require('redis');
var redisStore = require('connect-redis')(session);
var client = redis.createClient();
var app = express();
app.use(session({
secret: 'shhhhh',
store: new redisStore({ host: 'localhost', port: 6379, client: client,ttl : 260}),
saveUninitialized: false,
resave: false
}));
app.set('view engine','ejs');
app.use('/assets',express.static('./public/assets'));
mailcontroller();
//listen to port
app.listen(3000);
是否可以使用应用程序而不是req来访问会话的用户名?,因为我负担不起写信
module.exports = function(app){
app.get('/',function(req,res){
var sendEmail = node.scheduleJob('*/60 * * * *', function(){
pro.find({username:"varunreddy"},function(err,data){
});
};
在 mailcontroller 中,因为它仅在用户登录后才安排邮件!!
答案 0 :(得分:1)
请使用以下命令获取会话跟踪 Ssn =请求会话; Ssn.client;