本文介绍了使用Restify时如何支持cors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用restify模块创建的REST api,我想允许跨域资源共享.最好的方法是什么?

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?

推荐答案

您必须将服务器设置为设置跨源标头.不确定是否有内置使用功能,所以我写了自己的功能.

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();
  }
);

我从本教程中发现了这一点. http://backbonetutorials.com/nodejs-restify-mongodb-mongoose/

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

这篇关于使用Restify时如何支持cors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 01:48