Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 1.04 KB

cors_management.md

File metadata and controls

40 lines (32 loc) · 1.04 KB

CORS Management

back to index

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.

References

back to index