我正在尝试在我的快递应用程序中使用redis进行会话。
我执行以下操作:
server / 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);
});
文件server / auth.js的console_log()让我“未定义”。
我正在使用Este JS框架。
有人可以帮助我吗?
谢谢!
答案 0 :(得分:1)
问题解决了。 isomorphic-fetch不会将Cookie发送到服务器
https://github.com/matthew-andrews/isomorphic-fetch/issues/89