CORS Configuration

Configure CORS (Cross-Origin Resource Sharing) in moclojer for local development, testing, and cross-domain API access with proper headers.

Configure CORS headers in moclojer for cross-origin requests.

🎯 Enable CORS

Environment Variable

MOCLOJER_ENABLE_CORS=true moclojer --config mocks.yml

Manual Headers

- endpoint:
    method: OPTIONS
    path: /api/users
    response:
      status: 204
      headers:
        Access-Control-Allow-Origin: "*"
        Access-Control-Allow-Methods: "GET, POST, PUT, DELETE, OPTIONS"
        Access-Control-Allow-Headers: "Content-Type, Authorization"
        Access-Control-Max-Age: "86400"
      body: ""

- endpoint:
    method: GET
    path: /api/users
    response:
      status: 200
      headers:
        Access-Control-Allow-Origin: "*"
        Content-Type: application/json
      body: >
        {"users": []}

πŸ”’ Specific Origins

βœ… Best Practices

Development:

  • Use Access-Control-Allow-Origin: "*"

Production:

  • Specify exact origins

  • Avoid wildcards

  • Use credentials carefully

πŸ“š See Also

Last updated

Was this helpful?