Friday 28 April 2017

Simplest Node API possible, still can't get cookies setting when front and backend separately deployed on Heroku - though set-cookie headers do send, cors is handled, and set proxy is enabled.

I have this API deployed to Heroku:const express = require('express') const cors = require('cors') const app = express() app.set('trust proxy', 1) const corsOptions = { 'origin': true, 'credentials': true, } app.options('*', cors(corsOptions)) app.use(cors(corsOptions)) const port = process.env.PORT || 8080 app.post('/', function (req, res) { res.cookie("firstTestCookie", {curious: "george"}) res.cookie("secondTestCookie", { charlottes: "web", the: "lorax" }, { encode: String, credentials: 'include' }) res.send('test sent') }) app.listen(port, () => { console.log(`listening on port ${port}`) }) On a separate dyno, I have the frontend which is nothing but a button that does this: function buttonClickFetch() { fetch(heroku_api_url, { method: 'POST', credentials: 'include', }) .then((response) => { return response.text() }) .then((text) => { console.log({res: text}) }) .catch((er) => { console.log({error: er}) }) } Cors is working fine, I get no cors warnings in the browser (Chrome) which I would otherwise have if I got rid of the cors stuff in the API. In fact, the set-cookie headers are visible in the browser dev tools > network tab for both cookies. They just won't set!

Submitted April 28, 2017 at 06:23PM by coderbee

No comments:

Post a Comment