详解Nodejs get获取远程服务器接口数据


Posted in NodeJs onMarch 26, 2019

本文实例为大家分享了Nodejs get获取远程服务器接口数据的具体代码,供大家参考,具体内容如下

1.GET模块:_get.js

/**
 * Created by jinx on 7/7/17.
 */
var http = require('http');

module.exports = {
  /**
  * 测试获取所有的区域
  * /
  locations: function (cb) {
    http.get('http://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')
  , http = require('http');

var app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
/**
 * 静态文件目录
 */
app.use(express.static('public'));
/**
 * 加载路由配置
 */
routes(app,__dirname);
/**
 * 启动服务器
 */
http.createServer(app).listen(app.get('port'), function(){
 console.log("服务器已经启动了" + app.get('port'));
});

4.项目目录如下:

详解Nodejs get获取远程服务器接口数据

5.调用js get.js:

/**
 * Created by jinx on 7/7/17.
 */
var _i;
$(function () {
  _i = layer.open({type: 2});
  $.ajax({
    url: '/get',
    type: 'get',
    dataType: 'json',
    success: function (res) {
      if (res != null)
        layer.close(_i);
      new Vue({
        el: '.main',
        data: {items: res.params}
      });
    }
  })
})

6.调用页面 get.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <title>http get</title>
  <link href="https://cdn.bootcss.com/layer/3.0.1/mobile/need/layer.min.css" rel="external nofollow" rel="stylesheet">
  <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="external nofollow" rel="stylesheet">
  <link href="https://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="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.bootcss.com/layer/3.0.1/mobile/layer.js"></script>
<script src="https://cdn.bootcss.com/vue/2.3.4/vue.min.js"></script>
<script src="js/get.js"></script>
</body>
</html>

以上所述是小编给大家介绍的Nodejs get获取远程服务器接口数据详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

NodeJs 相关文章推荐
NodeJS的模块写法入门(实例代码)
Mar 07 NodeJs
跟我学Nodejs(三)--- Node.js模块
May 25 NodeJs
nodejs URL模块操作URL相关方法介绍
Mar 03 NodeJs
详解nodejs 文本操作模块-fs模块(五)
Dec 23 NodeJs
nodejs+express实现文件上传下载管理网站
Mar 15 NodeJs
手把手教你把nodejs部署到linux上跑出hello world
Jun 19 NodeJs
nodejs socket服务端和客户端简单通信功能
Sep 14 NodeJs
nodejs 简单实现动态html的方法
May 12 NodeJs
webstorm中配置nodejs环境及npm的实例
May 15 NodeJs
NodeJs生成sitemap站点地图的方法示例
Jun 11 NodeJs
nodejs对项目下所有空文件夹创建gitkeep的方法
Aug 02 NodeJs
如何利用nodejs自动定时发送邮件提醒(超实用)
Dec 01 NodeJs
nodejs微信开发之自动回复的实现
Mar 17 #NodeJs
nodejs微信开发之接入指南
Mar 17 #NodeJs
nodejs微信开发之授权登录+获取用户信息
Mar 17 #NodeJs
详解nodejs 开发企业微信第三方应用入门教程
Mar 12 #NodeJs
详解NodeJS Https HSM双向认证实现
Mar 12 #NodeJs
NodeJs入门教程之定时器和队列
Mar 08 #NodeJs
nodejs npm错误Error:UNKNOWN:unknown error,mkdir 'D:\Develop\nodejs\node_global'at Error
Mar 02 #NodeJs
You might like
php+mysql写的简单留言本实例代码
2008/07/25 PHP
PHP统计目录大小的自定义函数分享
2014/11/18 PHP
初识通用数据库操作类――前端easyui-datagrid,form(php)
2015/07/31 PHP
php删除数组指定元素实现代码
2017/05/03 PHP
PHP 图片处理
2020/09/16 PHP
jquery 中多条件选择器,相对选择器,层次选择器的区别
2012/07/03 Javascript
解读JavaScript中 For, While与递归的用法
2013/05/07 Javascript
node.js中的fs.chownSync方法使用说明
2014/12/16 Javascript
jQuery网页选项卡插件rTabs用法实例分析
2015/08/26 Javascript
JavaScript对象参数的引用传递
2016/01/14 Javascript
JavaScript中数组添加值和访问值常见问题
2016/02/06 Javascript
JS实现重新加载当前页面
2016/11/29 Javascript
Angular中点击li标签实现更改颜色的核心代码
2017/12/08 Javascript
微信小程序实现的涂鸦功能示例【附源码下载】
2018/01/12 Javascript
Mac下通过brew安装指定版本的nodejs教程
2018/05/17 NodeJs
centos 上快速搭建ghost博客方法分享
2018/05/23 Javascript
[01:23:24]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Elephant BO3 第三场 2月7日
2021/03/11 DOTA
分析python服务器拒绝服务攻击代码
2014/01/16 Python
Python脚本实现12306火车票查询系统
2016/09/30 Python
Python用list或dict字段模式读取文件的方法
2017/01/10 Python
Python调用C++程序的方法详解
2017/01/24 Python
django使用图片延时加载引起后台404错误
2017/04/18 Python
Python实现PS滤镜的万花筒效果示例
2018/01/23 Python
Django 1.10以上版本 url 配置注意事项详解
2019/08/05 Python
HTML5中微数据概述及在搜索引擎中的使用举例
2013/02/07 HTML / CSS
美国成衣女装品牌:CHICO’S
2016/09/19 全球购物
欧洲有机婴儿食品最大的市场:Organic Baby Food(供美国和加拿大)
2018/03/28 全球购物
成功的酒店创业计划书
2013/12/27 职场文书
办护照工作证明范本
2014/01/14 职场文书
三年级评语大全
2014/04/23 职场文书
生物技术专业求职信
2014/06/10 职场文书
2014年机关工会工作总结
2014/12/19 职场文书
端午节活动总结报告
2015/02/11 职场文书
喋血孤城观后感
2015/06/08 职场文书
严以修身专题学习研讨会发言材料
2015/11/09 职场文书
数据结构课程设计心得体会
2016/01/15 职场文书