1

I've read this question and my problem seems similar. I have an heroku remote app and I've added the host permission to the manivest v3 of my vue extension.

I need to send a post request from the chrome extension to send notifications, but I'm facing a CORS issue. How I can setup correctly express to use cors? I need to change something in my manifest or chrome extension code?

//express server for heroku fcm extension 

const express = require('express')
const app = express()
const axios = require('axios')
const port = process.env.PORT || 3000 
const cors = require('cors')({ origin: true })

const serverKey = 'key=AAA...'

//
app.get('/', (req, res) => {
// redirect to chrome web store
    res.send('Service Running!')
})

app.post('/sendNotification', (req, res) => {
    //cors( (req, res) => {

    
        console.log(req)
        const dest = req.body.to
        const notification = req.body.notification

        axios.post('https://fcm.googleapis.com/fcm/send', {
            to: dest,
                notification: notification
            }, {
                headers: {
                    'Authorization': serverKey,
                    'Content-Type': 'application/json'
                }
            }).then( (response) => {
                console.log(response)
                res.send(response)   
            })
    //})
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})
{
    "manifest_version": 3,
    "name": "push",
    "description": "chrome extension",
    "version": "1.0",
    "background": {
        "service_worker": "background.js"
    },
    "action": {},
    "permissions": [
        "gcm",
        "identity"
    ],
    "host_permissions": [
        "https://<...>.herokuapp.com/*"
    ],
    "oauth2": {
        "client_id": "...apps.googleusercontent.com",
        "scopes": [
          "https://www.googleapis.com/auth/userinfo.email",
          "https://www.googleapis.com/auth/userinfo.profile"
        ]
    }
}

I'm able to deploy the app on heroku, and when I get the cors issue on the fe I will get a 200 response in server ?

enter image description here

newbiedev
  • 2,607
  • 3
  • 17
  • 65
  • 1
    what do you expect `require('cors')({ origin: true })` to do? Origin has to be either `*` to allow all origins, or a specific origin like `https://myurl.com:12345` or some function with a callback (see the docs for details). `true` doesn't make any sense as value for `origin` Furthermore, you are `require`ing `cors` but you don't `app.use(...)` it anywhere. – derpirscher Jul 26 '22 at 15:50
  • @derpirscher since I've used CORS package in past with [cloud function](https://stackoverflow.com/questions/42755131/enabling-cors-in-cloud-functions-for-firebase), usually I use `require('cors')({ origin: true })` and it work. Anyway I've solved. – newbiedev Jul 28 '22 at 08:45

0 Answers0