List of values in a session using NodeJS

2

I'm using the express-session and express modules and I need to create a list of values in an item in a session so that at some point you can add values to it. Ex:

req.session.category="Include key and value in this property"

Could someone tell me how to do this?

    
asked by anonymous 29.03.2017 / 20:35

1 answer

0

I solved using the following code below:

var app = require('express')();
var http = require('http').Server(app);
var session = require('express-session');
var express = require('express');

app.set('trust proxy', 1) // trust first proxy

app.use(session({
secret: 's3ssIon',
resave: false,
saveUninitialized: true
}));

app.get('/', function (req, res) {

req.session.formula = [];

req.session.views = (req.session.views || 0) + 1

req.session.formula.push(req.session.views);

res.end(req.session.views + ' views')

});

app.get('/resultado', function (req, res) {

req.session.views = (req.session.views || 0) + 1

req.session.formula.push(req.session.views);

res.end(req.session.views + ' views')

});

app.get('/fim', function (req, res) {

req.session.destroy();

res.end('Session encerrada')
});

http.listen(3000, function () {
console.log("Connected");
});

I configured the Session, created 3 routes (root, result and end), I used [] to create the array and the push method to include the values.

    
24.04.2017 / 19:20