简体   繁体   中英

Express.js don't set cookies on production server

I've collided with issue in my server.js script. Here is the server.js code:

require('dotenv').config();

const express = require('express');
const app = express();
const appPort = process.env.APP_PORT;

app.get('/', (req, res) => {

    var campaign = req.query.campaign;
    var click = req.query.param1;

    if (campaign !== undefined && click !== undefined)
    {
        res.cookie('campaign', campaign, { maxAge: 86400 * 1000 });
        res.cookie('click', click, { maxAge: 86400 * 1000 });
    }
    res.sendFile(__dirname + '/dist/index.html');
});

app.get('/members', (req, res) => res.sendFile(__dirname + '/dist/members.html'));

app.post('/members', function (req, res) {
    res.cookie('firstName', req.body.firstname);
    res.cookie('email', req.body.email);

    res.sendFile(__dirname + '/dist/members.html');
});

app.listen(appPort, () => console.log(`App is running on ${appPort} port`));
app.use(express.static('dist')); 

I have to set cookies when the next request will come to a server:

http://example.com/?campaign=28&param1=c4324

All work fine at my localhost, but when I transfer all changes to production server all work fine to, except a cookies, witch don't saving at the app.get('/', (req, res) . In other places (for example: app.get('/members') all work fine. Please help me.

Problem was in OS configuration, more precisely in access rights. I use digitalocean service, where I setup Ubuntu, Apache and other staff. When my app tried to create cookies, Ubuntu don't allow Apache default user to create it. When I allowed Apache user to write new files in my app directory the problem is solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM