List of values in a Session using Nodejs

Asked

Viewed 358 times

2

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

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

Could someone tell me how to do this?

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 set up Session, created 3 routes (root, result and end), used [] to create the array and push method to include the values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.