To allow a React (or similiar) frontend to access the backend API, you need to setup a CORS middleware in your vapor configure.swift
file:
let corsMiddleware = CORSMiddleware(
configuration: CORSMiddleware.Configuration.init(
allowedOrigin: .all,
allowedMethods: [
.POST,
.GET,
.PUT,
.OPTIONS,
.DELETE,
.PATCH
],
allowedHeaders: [
.accept,
.authorization,
.contentType,
.origin,
.xRequestedWith
]))
middlewares.use(corsMiddleware)
Note: This is the default parameters expanded so you can see them without digging into the code. Maybe you'll need to adjust the parameters. That said, it has been tested and this example allows a default React website to access the vapor API routes.