Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【20161020】NodeJS转发Ajax请求 #36

Closed
zhongxia245 opened this issue Oct 20, 2016 · 2 comments
Closed

【20161020】NodeJS转发Ajax请求 #36

zhongxia245 opened this issue Oct 20, 2016 · 2 comments

Comments

@zhongxia245
Copy link
Owner

zhongxia245 commented Oct 20, 2016

请求某一个接口,然后后期可能换接口了。 如何转发接口呢?
解决该问题呢,可以用服务端来进行请求该接口,然后把返回值原封不动的返回即可。

下面是使用 NodeJs + Express 来实现转发Ajax请求的代码

var express = require('express');
var router = express.Router();
var request = require('request');

/**
 * NodeJs 转发 接口请求
 * 使用方式:127.0.0.1:23018/request?url=完整的接口地址
 */
router.get('/', function (req, res, next) {
  req.pipe(request.get(req.query.url, {
    query: req.query,
    form: req.body
  })).pipe(res);
});

module.exports = router;
@zhongxia245
Copy link
Owner Author

这个转发http请求,只是把 访问 A 地址的接口, 转到 去请求 B接口

@zhongxia245
Copy link
Owner Author

Node转发请求,解决跨域问题, 可以看这个类库
node-http-proxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant