Node.js快速会话中间件在Visual Studio中不起作用

时间:2019-06-19 14:31:14

标签: node.js express visual-studio-2017 express-session

我正在尝试在nodejs express中使用express-session,但无法设置或获取会话。尝试构建时,出现“请求类型上不存在属性'会话'。”

我试图更改代码的顺序,但无济于事

节点版本:v10.15.3

视觉工作室:2017

我的依赖项

 "dependencies": {
    "axios": "^0.19.0",
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.4",
    "debug": "^2.2.0",
    "express": "^4.14.0",
    "express-session": "^1.16.2",
    "pug": "^2.0.0-rc.3",
    "session-file-store": "^1.3.0"
  },
  "devDependencies": {
    "@types/debug": "0.0.30",
    "@types/express": "^4.0.37",
    "@types/express-serve-static-core": "^4.0.50",
    "@types/mime": "^1.3.1",
    "@types/serve-static": "^1.7.32",
    "@types/node": "^6.0.87"
  }

app.ts

  var app = express();
  var bodyParser = require('body-parser');
  var session = require('express-session');
  var FileStore = require('session-file-store')(session);

  app.use(bodyParser.urlencoded({extended: true})); 
  app.use(bodyParser.json());       // to support JSON-encoded bodies
  app.use(express.json());       // to support JSON-encoded bodies
  app.use(express.urlencoded({ extended: true })); // to support URL-encoded   bodies
  app.use(session({
      name: 'test-session-cookie-id',
      secret: 'test',
      resave: true,
      store: new FileStore(),
      saveUninitialized: true,
      cookie: { secure: true }
  }));
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'pug');

  app.use(express.static(path.join(__dirname, 'public')));


  app.use('/', routes);
  app.use('/users', users);
  app.use('/signup', signup);
  app.use('/verify', verify);

  app.use(function printSession(req, res, next) {
    console.log('req.session', req.session); //error
    return next();
  });

更新:

模块本身似乎运行良好。它创建了一个包含500多个文件的会话文件夹

update2:

快速会议运行正常,但错误困扰着我。我卸载了vs2017并安装了vs2019并更新了打字稿,但问题仍然存在

1 个答案:

答案 0 :(得分:0)

我的@types/express-session中没有dependencies/devDependenciesnpm install --save @types/express-session完成了工作。现在没有错误