Skip to content

Instantly share code, notes, and snippets.

@uknownothingsnow
Forked from seansullivan/gist:4507044
Last active December 25, 2015 18:29
Show Gist options
  • Save uknownothingsnow/7020717 to your computer and use it in GitHub Desktop.
Save uknownothingsnow/7020717 to your computer and use it in GitHub Desktop.
allowCrossDomain middleware for expressjs or compundjs
#CORS middleware
allowCrossDomain = (req, res, next) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Methods', 'OPTIONS,GET,PUT,POST,DELETE'
res.header 'Access-Control-Allow-Headers', 'Content-Type, X-Requested-With'
next()
# Cut off OPTIONS requests and just send true as response
handleOptionsMethod = (req, res, next) ->
return res.send(200) if req.method == 'OPTIONS'
next()
module.exports = (compound) ->
express = require 'express'
app = compound.app
app.configure ->
# removed config above for conciseness ...
app.use allowCrossDomain
app.use handleOptionsMethod
# removed config below for conciseness ...
@uknownothingsnow
Copy link
Author

allowcrossdomain middleware for express and compoundjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment