Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SERVER CODE TO BE PUT INSIDE NODE APP
- const cors = require('cors')
- const express = require('express')
- const cookieParser = require('cookie-parser')
- const app = express()
- const PORT = process.env.PORT || 8080
- const whitelist = ['http://192.168.1.17:5173', 'http://localhost:5173']
- const corsOptions = {
- credentials: true,
- optionSuccessStatus: 200,
- origin: (origin, callback) => {
- console.log('origin is : ', origin)
- if (whitelist.includes(origin)) return callback(null, true)
- callback(new Error('Not allowed by CORS'))
- },
- }
- app.use(cors())
- app.use(cookieParser())
- app.get('', (req, res) => {
- console.log('req.headers.cookie : ', req.headers.cookie, "and parser's req.cookie is : ", req.cookies)
- res.status(200).send('Request complete')
- })
- app.listen(PORT, () => console.log('Node alive and listening. Your app is running on port', PORT))
- // CLIENT CODE SNIPPET TO BE PUT INSIDE ANY REACT APP COMPONENT
- <input
- type={'button'}
- onClick={() => {
- fetch('http://localhost:8080', {
- mode: 'cors',
- credentials: 'include',
- })
- .then((res) => console.log('done', res.status))
- .catch((err) => console.log('bad', err))
- }}
- />
Advertisement
Add Comment
Please, Sign In to add comment