Skip to content

Instantly share code, notes, and snippets.

@lzyzsd
Forked from seansullivan/gist:4507044
Last active December 25, 2015 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lzyzsd/7020717 to your computer and use it in GitHub Desktop.
Save lzyzsd/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 ...
@lzyzsd
Copy link
Author

lzyzsd commented Oct 17, 2013

allowcrossdomain middleware for express and compoundjs

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