如何通过Node.js的GET请求高效获取远程服务器接口数据?
- 内容介绍
- 文章标签
- 相关推荐
本文共计571个文字,预计阅读时间需要3分钟。
javascriptNode.js 获取远程服务器接口数据的示例代码如下:
1. GET 模块示例:javascript// get.js/** * Created by jinx on 7/7/17. */
const http=require('http');
module.exports={ /** * 测试 */ test: ()=> { http.get('http://example.com/api/data', (res)=> { let data=''; res.on('data', (chunk)=> { data +=chunk; }); res.on('end', ()=> { console.log(data); }); }).on('error', (e)=> { console.error(`problem with request: ${e.message}`); }); }};
本文实例为大家分享了Nodejs get获取远程服务器接口数据的具体代码,供大家参考,具体内容如下
1.GET模块:_get.js
/** * Created by jinx on 7/7/17. */ var wx.xx.com/locations', function (res) { res.setEncoding('utf8'); var rawData = ''; res.on('data', function (chunk) { rawData += chunk; }); res.on('end', function () { try { const parsedData = JSON.parse(rawData); console.log(parsedData); cb(parsedData); } catch (e) { console.error(e.message); cb('error'); } }); }); } }
2.路由端调用:routes.js
var _get = require('../modules/_get'); module.exports = function (app, _dirpath) { app.get('/get', function (req, res) { _get.locations(function (data) { res.writeHead(200, {"Content-Type": "application/json"}); res.write(JSON.stringify(data)); res.end(); }); }); }
3.服务启动入口:
/** * Created by jinx on 7/3/17. */ var express = require('express') , routes = require('./routes/routes') , cdn.bootcss.com/layer/3.0.1/mobile/need/layer.min.css" rel="external nofollow" rel="stylesheet"> <link href="cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="external nofollow" rel="stylesheet"> <link href="cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"> <link href="css/style.css" rel="external nofollow" rel="stylesheet"> </head> <body> <table class="table main"> <thead> <tr> <td>ID</td> <td>Name</td> </tr> </thead> <tbody> <tr v-for="item in items" > <td v-text="item.id"></td> <td v-text="item.name"></td> </tr> </tbody> </table> <a href="/" rel="external nofollow" class="btn btn-info width-100">返回首页</a> <script src="img.558idc.com/uploadfile/allimg/210605/1914194494-1.jpg"></script> <script src="img.558idc.com/uploadfile/allimg/210605/1914191F2-2.jpg"></script> <script src="img.558idc.com/uploadfile/allimg/210605/1914192a2-3.jpg"></script> <script src="img.558idc.com/uploadfile/allimg/210605/191419A64-4.jpg"></script> <script src="js/get.js"></script> </body> </html>
以上所述是小编给大家介绍的Nodejs get获取远程服务器接口数据详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自由互联网站的支持!
本文共计571个文字,预计阅读时间需要3分钟。
javascriptNode.js 获取远程服务器接口数据的示例代码如下:
1. GET 模块示例:javascript// get.js/** * Created by jinx on 7/7/17. */
const http=require('http');
module.exports={ /** * 测试 */ test: ()=> { http.get('http://example.com/api/data', (res)=> { let data=''; res.on('data', (chunk)=> { data +=chunk; }); res.on('end', ()=> { console.log(data); }); }).on('error', (e)=> { console.error(`problem with request: ${e.message}`); }); }};
本文实例为大家分享了Nodejs get获取远程服务器接口数据的具体代码,供大家参考,具体内容如下
1.GET模块:_get.js
/** * Created by jinx on 7/7/17. */ var wx.xx.com/locations', function (res) { res.setEncoding('utf8'); var rawData = ''; res.on('data', function (chunk) { rawData += chunk; }); res.on('end', function () { try { const parsedData = JSON.parse(rawData); console.log(parsedData); cb(parsedData); } catch (e) { console.error(e.message); cb('error'); } }); }); } }
2.路由端调用:routes.js
var _get = require('../modules/_get'); module.exports = function (app, _dirpath) { app.get('/get', function (req, res) { _get.locations(function (data) { res.writeHead(200, {"Content-Type": "application/json"}); res.write(JSON.stringify(data)); res.end(); }); }); }
3.服务启动入口:
/** * Created by jinx on 7/3/17. */ var express = require('express') , routes = require('./routes/routes') , cdn.bootcss.com/layer/3.0.1/mobile/need/layer.min.css" rel="external nofollow" rel="stylesheet"> <link href="cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="external nofollow" rel="stylesheet"> <link href="cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"> <link href="css/style.css" rel="external nofollow" rel="stylesheet"> </head> <body> <table class="table main"> <thead> <tr> <td>ID</td> <td>Name</td> </tr> </thead> <tbody> <tr v-for="item in items" > <td v-text="item.id"></td> <td v-text="item.name"></td> </tr> </tbody> </table> <a href="/" rel="external nofollow" class="btn btn-info width-100">返回首页</a> <script src="img.558idc.com/uploadfile/allimg/210605/1914194494-1.jpg"></script> <script src="img.558idc.com/uploadfile/allimg/210605/1914191F2-2.jpg"></script> <script src="img.558idc.com/uploadfile/allimg/210605/1914192a2-3.jpg"></script> <script src="img.558idc.com/uploadfile/allimg/210605/191419A64-4.jpg"></script> <script src="js/get.js"></script> </body> </html>
以上所述是小编给大家介绍的Nodejs get获取远程服务器接口数据详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自由互联网站的支持!

