【发布时间】:2021-03-21 14:49:28
【问题描述】:
const whitelist = ["http://mywebsite.com", "http://localhost:5000"]
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
const cors = require('cors')(corsOptions);
exports.getUserProfile = functions.https.onRequest((req, res) => {
cors(req, res, () => {
try {
//... here we fetch the user profile and send it in response
} catch (err) {
res.json({
status: "error",
message: err
})
}
});
});
我以这种方式使用 CORS,所以我只能让我自己的网站和 localhost:5000 向我正在使用的云功能发出请求。 localhost:5000 适合我,所以我可以在部署文件之前在本地测试东西。
- 从 reqbin.com 向我的云功能发出请求有效,为什么?它不应该不起作用吗,因为我已将域指定为来自 mywebsite.com 或 localhost:5000?
- 是否有任何其他方法可以在本地测试内容而不必将 localhost:5000 包含在白名单中?我正在使用
firebase serve进行测试,但我假设这不起作用,因为来自 mywebsite.com 的请求不会被处理
【问题讨论】:
标签: javascript node.js firebase google-cloud-functions cors