2
我正在嘗試在我的快速應用中使用redis進行會話。'session'是未定義的 - Este JS/Express/Redis會話存儲
我做到以下幾點:
服務器/ auth.js
import express from 'express';
import uuid from 'uuid';
const router = express.Router();
router.route('/login').post((req, res) => {
const { password } = req.body;
if (password !== 'pass1') {
res.status(401).end();
} else {
const token = uuid.v4();
req.session.token = token;
res.status(200).send({token}).end();
}
});
router.route('/watch').post((req, res) => {
console.log(req.session.token);
res.status(200).send({}).end();
});
router.route('/logout').post((req, res) => {
...
});
export default router;
服務器/ api.js
import express from 'express';
import session from 'express-session';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import cors from 'cors';
import redis from 'redis';
import connectRedis from 'connect-redis';
import auth from './auth';
const app = express();
let client = redis.createClient();
let RedisStore = connectRedis(session);
app.use(cors());
app.use(session({
secret: 'ssshhhhh',
store: new RedisStore({ host: 'localhost', port: 6379, client: client, ttl: 260}),
saveUninitialized: false,
resave: false
}));
app.use(cookieParser("secretSign#143_!223"));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use('/auth', auth);
app.on('mount',() => {
console.log('Api is available at %s', app.mountpath);
});
export default app;
服務器/ main.js
import express from 'express';
import api from './api';
import frontend from './frontend';
import errorHandler from './lib/errorHandler';
import config from './config';
const app = express();
app.use('/api/v1', api);
app.use(frontend);
app.use(errorHandler);
app.listen(config.port,() => {
console.log('Server started at port %d', config.port);
});
而文件服務器/ auth.js的console_log()讓我「未定義」。
我正在使用Este JS框架。
有人能幫助我嗎?
謝謝!