【问题标题】:'session' is undefined - Este JS / Express / Redis for session store“会话”未定义 - 用于会话存储的 Este JS / Express / Redis
【发布时间】:2016-09-07 12:41:33
【问题描述】:

我正在尝试在我的 express 应用中使用 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;

server/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 框架。

谁能帮帮我?

谢谢!

【问题讨论】:

    标签: javascript node.js session express redis


    【解决方案1】:

    问题解决了。 isomorphic-fetch 不发送 Cookie 到服务器

    https://github.com/matthew-andrews/isomorphic-fetch/issues/89

    【讨论】:

      猜你喜欢
      • 2012-04-28
      • 1970-01-01
      • 2013-12-13
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多