I have deployed this express rest API using netlify https://vtokback.netlify.app/.netlify/functions/server/
.
When ever I try to make a GET or PULL request using either postman or react or even simply curl, it returns this error:
Access to XMLHttpRequest at 'https://vtokback.netlify.app/.netlify/functions/serverusers/register' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
This is my netlify.toml file in the root of the server directory:
`[build]
functions=“functions”
[[headers]]
for = “/"
[headers.values]
Access-Control-Allow-Origin = "”
and this is my server.js in functions
const express = require(“express”);
const serverless = require(“serverless-http”);
const cors = require(“cors”);
const mongoose = require(“mongoose”);
const app = express();
const router = express.Router();
const dbUrl =
“db url is here”;
const corsOptions = {
origin: ‘http://localhost:5173’,
credentials: false,
optionSuccessStatus: 200
}
app.use(cors(corsOptions));
app.use(express.json());
mongoose.connect(dbUrl);
const userRouter = require(“…/routes/users”);
const messageRouter = require(“…/routes/messages”);
router.use(“/users”, userRouter);
router.use(“/messages”, messageRouter);
router.get(“/”, (req, res) => {
res.send(“You got to the root. Yay!”);
});
// Define your routes
app.use(“/.netlify/functions/server”, router);
module.exports.handler = serverless(app);
`
I can get the pages from the browser but not programmatically. BTW this is how I am getting and posting onto the server (using axios)
axios.get(“https://vtokback.netlify.app/.netlify/functions/server/messages”)
It all works fine when I was running it on localhost but now it just doesnt’ work.