I am using angular (frontend) and node.js + express (backend). When I run my app on localhost:3000
(the port the express app is runing on) everything is correct and I notice that the request session ID is the same even if I refresh the page. However when I want to work in dev mode on localhost:4200
the session is not persistent anymore and each time I refresh the page I have a new session ID displayed.
app.js
const bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
cors = require('cors'),
express = require('express'),
session = require('express-session');
const app = express();
app.use(cors());
const port = process.env.PORT || '3000';
app.set('port', port);
const server = http.createServer(app);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(session({
secret: 'MY-KEY',
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
app.use(passport.initialize());
app.use(passport.session());
server.listen(port, () => console.log(`API running on localhost:${port}`));
I am also using a proxy when in dev mode as below:
proxy-conf.json
{
"/api/*": {
"target": "http://localhost:3000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}