{"id":17951862,"url":"https://github.com/superman66/node-proxy-api","last_synced_at":"2025-03-25T00:31:36.804Z","repository":{"id":96365434,"uuid":"85139231","full_name":"superman66/node-proxy-api","owner":"superman66","description":"The simple node proxy api ","archived":false,"fork":false,"pushed_at":"2017-04-26T07:02:04.000Z","size":5,"stargazers_count":16,"open_issues_count":0,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-19T05:41:57.248Z","etag":null,"topics":["api","express","nodejs","proxy"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/superman66.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-16T01:39:20.000Z","updated_at":"2023-08-31T06:08:32.000Z","dependencies_parsed_at":"2023-05-12T14:31:13.901Z","dependency_job_id":null,"html_url":"https://github.com/superman66/node-proxy-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superman66%2Fnode-proxy-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superman66%2Fnode-proxy-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superman66%2Fnode-proxy-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/superman66%2Fnode-proxy-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/superman66","download_url":"https://codeload.github.com/superman66/node-proxy-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245377920,"owners_count":20605374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","express","nodejs","proxy"],"created_at":"2024-10-29T09:51:28.910Z","updated_at":"2025-03-25T00:31:36.796Z","avatar_url":"https://github.com/superman66.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Node + Express + superagent 转发 Api 请求\n简单的几行代码实现如何通过Node + Express + superagent 转发 API 请求。已部署到 Heroku。\n\n**项目中用到了豆瓣电影 Api, 更多 Api请见 [豆瓣Api文档](https://developers.douban.com/wiki/?title=api_v2)**\n\n**安装依赖**\n\nNode.js 转发请求用到了 `express`和`superagent`. [superanget](https://github.com/visionmedia/superagent)是一个 Node.js HTTP client。\n```\nnpm install express superagent --save\n```\n\n**端口设置**\n\n由于部署到 Heroku 时，端口是动态分配的，所以需要根据 `process.env.NODE_ENV` 动态设置端口:\n```javascript\napp.set('port', (process.env.PORT || 5000));\n```\n如果不需要部署到 Heroku，端口可直接写死。\n**定义接口**\n\n根据前端所需，定义了如下三个接口：\n```javascript\napp.get('/movie/:type', function (req, res) {\n  var sreq = request.get(HOST + req.originalUrl)\n  sreq.pipe(res);\n  sreq.on('end', function (error, res) {\n    console.log('end');\n  });\n})\n\napp.get('/movie/subject/:id', function (req, res) {\n  var sreq = request.get(HOST + req.originalUrl)\n  sreq.pipe(res);\n  sreq.on('end', function (error, res) {\n    console.log('end');\n  });\n})\n\napp.get('/movie/search', function (req, res) {\n  var sreq = request.get(HOST + req.originalUrl)\n  sreq.pipe(res);\n  sreq.on('end', function (error, res) {\n    console.log('end');\n  });\n})\n```\n\n**CORS设置**\n\n\u003e跨源资源共享 ( [CORS](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS) )机制让Web应用服务器能支持跨站访问控制，从而使得安全地进行跨站数据传输成为可能。\n主要是通过设置`Access-Control-Allow-Origin: *`\n```javascript\napp.all('*', function (req, res, next) {\n  if (!req.get('Origin')) return next();\n  // use \"*\" here to accept any origin\n  res.set('Access-Control-Allow-Origin', '*');\n  res.set('Access-Control-Allow-Methods', 'GET');\n  res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');\n  // res.set('Access-Control-Allow-Max-Age', 3600);\n  if ('OPTIONS' == req.method) return res.send(200);\n  next();\n});\n```\n**端口监听**\n\n```javascript\napp.listen(app.get('port'), function() {\n  console.log('Node app is running on port', app.get('port'));\n});\n```\n**启动**\n\n```\ncd node-proxy\nnode index.js\n```\n\n具体见`node-proxy/index.js`\n\n# 部署到Heroku\n详情见： [官方 Getting Started](https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuperman66%2Fnode-proxy-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuperman66%2Fnode-proxy-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuperman66%2Fnode-proxy-api/lists"}