CORS error with worker to localhost:3000
error
worker
client
responds in postman
and when I spin up wrangler dev
but when trying to directly call https://users-worker.data-well.workers.dev/users from localhost:3000 it shows a cors error 😦
Access to fetch at 'https://users-worker.data-well.workers.dev/users' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Access to fetch at 'https://users-worker.data-well.workers.dev/users' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
import { Router } from 'itty-router'
import { withContent } from 'itty-router-extras'
import { Prisma, PrismaClient, User } from '@prisma/client/edge'
import { createCors } from 'itty-cors'
// create CORS handlers
const { preflight, corsify } = createCors({ origins: ['*'] })
const prisma = new PrismaClient()
const router = Router()
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json'
}
router.get('/users', async () => {
const response: User[] = await prisma.user.findMany()
return new Response(JSON.stringify(response), {
headers
})
})
router.get('/users/:id', async ({ params }) => {
const { id } = params;
const response = await prisma.user.findUnique({
where: {
id
}
})
return new Response(JSON.stringify(response), { headers })
})
router.post('/users', withContent, async ({ content }) => {
const {id, name, email} = content;
const response = await prisma.user.create({
data: {
id,
name,
email
}
})
return new Response(JSON.stringify(response), { headers })
})
router.all('*', preflight)
router.all('*', () => new Response('Not Found.', { status: 404 }))
addEventListener('fetch', event => {
event.respondWith(router.handle(event.request),)
})
import { Router } from 'itty-router'
import { withContent } from 'itty-router-extras'
import { Prisma, PrismaClient, User } from '@prisma/client/edge'
import { createCors } from 'itty-cors'
// create CORS handlers
const { preflight, corsify } = createCors({ origins: ['*'] })
const prisma = new PrismaClient()
const router = Router()
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-type': 'application/json'
}
router.get('/users', async () => {
const response: User[] = await prisma.user.findMany()
return new Response(JSON.stringify(response), {
headers
})
})
router.get('/users/:id', async ({ params }) => {
const { id } = params;
const response = await prisma.user.findUnique({
where: {
id
}
})
return new Response(JSON.stringify(response), { headers })
})
router.post('/users', withContent, async ({ content }) => {
const {id, name, email} = content;
const response = await prisma.user.create({
data: {
id,
name,
email
}
})
return new Response(JSON.stringify(response), { headers })
})
router.all('*', preflight)
router.all('*', () => new Response('Not Found.', { status: 404 }))
addEventListener('fetch', event => {
event.respondWith(router.handle(event.request),)
})
async getUsers(){
const response = await fetch('https://users-worker.data-well.workers.dev/users', {
method: 'get',
headers: { 'Access-Control-Allow-Origin' : '*' }
});
this.users = response
}
async getUsers(){
const response = await fetch('https://users-worker.data-well.workers.dev/users', {
method: 'get',
headers: { 'Access-Control-Allow-Origin' : '*' }
});
this.users = response
}
1 Reply
switched to hono and cors is working fine
itty-router / itty-cors 🤷♂️