Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I support cors when using restify

I have a REST api created with the restify module and I want to allow cross-origin resource sharing. What is the best way to do it?

like image 230
Kim Avatar asked Jan 15 '13 13:01

Kim


People also ask

How do I allow Cors Express?

Enabling CORS The easiest way to get CORS working in Express is by using the cors npm module. That's it. CORS is now enabled. The Access-Control-Allow-Origin header determines which origins are allowed to access server resources over CORS (the * wildcard allows access from any origin).

Why use restify?

Meet restify js web service framework optimized for building semantically correct RESTful web services ready for production use at scale. restify optimizes for introspection and performance, and is used in some of the largest Node. js deployments on Earth.


1 Answers

You have to set the server up to set cross origin headers. Not sure if there is a built in use function or not, so I wrote my own.

server.use(   function crossOrigin(req,res,next){     res.header("Access-Control-Allow-Origin", "*");     res.header("Access-Control-Allow-Headers", "X-Requested-With");     return next();   } ); 

I found this from this tutorial. http://backbonetutorials.com/nodejs-restify-mongodb-mongoose/

like image 125
Stephen Reid Avatar answered Oct 29 '22 15:10

Stephen Reid