昂首阔步不保留会议

时间:2018-10-04 05:09:39

标签: node.js express swagger

我正试图用招摇设置一个简单的登录端点:

//login.js

'use strict';

function login(req, res){
  const userId = req.swagger.params.user_id.value;
  const user = { 
    _id: userId
  }
  if(req.session.userId){
    return res.json([req.session.userId, 'hello']);
  }

  global.app.db.collection('users').insertOne(user, function(err, b){
    req.session.userId = userId;
    return res.json([req.session]);
  });
}

module.exports = {
  login: login
};

似乎if语句永远不会为真,并且永远不会永久设置userId(它仅出现在insert语句之后的第二个return语句中)。

不知道这里出了什么问题。我的app.js具有典型的快速会话设置。

//app.js
'use strict';
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const mongoDbUrl = 'mongodb://localhost:27017';
const dbName = 'my_db';


var SwaggerExpress = require('swagger-express-mw');
var app = require('express')();
var session = require('express-session')
const MongoStore = require('connect-mongo')(session);

app.set('trust proxy', 1) // trust first proxy
app.use(session({
  secret: 'some secret',
  store: new MongoStore({url:mongoDbUrl}),
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}))

module.exports = app; // for testing
global.app = app;

var config = {
  appRoot: __dirname // required config
};

SwaggerExpress.create(config, function(err, swaggerExpress) {
  if (err) { throw err; }

  // install middleware
  swaggerExpress.register(app);

  var port = process.env.PORT || 10010;
  app.listen(port);

  if (swaggerExpress.runner.swagger.paths['/hello']) {
    console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
  }
});

MongoClient.connect(mongoDbUrl, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);
  app.db = db;
});

1 个答案:

答案 0 :(得分:0)

看起来问题出在这行:

cookie: { secure: true }

安全性应该为假,因为我没有在本地计算机上使用https。