详解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的前后端分离的思考与实践(三)轻量级的接口配置建模框架
Sep 26 NodeJs
NodeJs——入门必看攻略
Jun 27 NodeJs
windows 下安装nodejs 环境变量设置
Feb 02 NodeJs
CentOS 安装NodeJS V8.0.0的方法
Jun 15 NodeJs
NodeJS自定义模块写法(详解)
Jun 27 NodeJs
详解nodejs的express如何自动生成项目框架
Jul 12 NodeJs
nodejs对express中next函数的一些理解
Sep 08 NodeJs
修改Nodejs内置的npm默认配置路径方法
May 13 NodeJs
如何让Nodejs支持H5 History模式(connect-history-api-fallback源码分析)
May 30 NodeJs
NodeJs实现简易WEB上传下载服务器
Aug 10 NodeJs
纯异步nodejs文件夹(目录)复制功能
Sep 03 NodeJs
浅谈Node的内存泄露问题
May 06 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.MVC的模板标签系统(二)
2006/09/05 PHP
php中支持多种编码的中文字符串截取函数!
2007/03/20 PHP
php+mysql实现简单登录注册修改密码网页
2016/11/30 PHP
jquery 入门教程 [翻译] 推荐
2009/08/17 Javascript
jQuery表单域选择器用法分析
2015/02/10 Javascript
javascript函数特点实例分析
2015/05/14 Javascript
JS+CSS实现滑动切换tab菜单效果
2015/08/25 Javascript
javascript面向对象程序设计高级特性经典教程(值得收藏)
2016/05/19 Javascript
浅谈js构造函数的方法与原型prototype
2016/07/04 Javascript
详细谈谈AngularJS的子级作用域问题
2016/09/05 Javascript
使用Vue-Router 2实现路由功能实例详解
2017/11/14 Javascript
Angular4.0中引入laydate.js日期插件的方法教程
2017/12/25 Javascript
30分钟快速入门掌握ES6/ES2015的核心内容(下)
2018/04/18 Javascript
详解SPA中前端路由基本原理与实现方式
2018/09/12 Javascript
vue3.0 CLI - 2.3 - 组件 home.vue 中学习指令和绑定
2018/09/14 Javascript
Bootstrap 按钮样式与使用代码详解
2018/12/09 Javascript
基于vue-cli、elementUI的Vue超简单入门小例子(推荐)
2019/04/17 Javascript
解决vue 单文件组件中样式加载问题
2019/04/24 Javascript
举例讲解Python中的身份运算符的使用方法
2015/10/13 Python
使用Python3制作TCP端口扫描器
2017/04/17 Python
python重要函数eval多种用法解析
2020/01/14 Python
在django admin中配置搜索域是一个外键时的处理方法
2020/05/20 Python
Python3 pywin32模块安装的详细步骤
2020/05/26 Python
HTML5+CSS3模仿优酷视频截图功能示例
2017/01/05 HTML / CSS
HTML5 3D旋转相册的实现示例
2019/12/03 HTML / CSS
美国折扣宠物药房:Total Pet Supply
2018/05/27 全球购物
FC-Moto瑞典:欧洲最大的摩托车服装和头盔商店之一
2018/11/27 全球购物
车辆维修工自我评价怎么写
2013/09/20 职场文书
总经理秘书的岗位职责
2013/12/27 职场文书
行政助理工作职责范本
2014/03/04 职场文书
医学专业大学生求职信
2014/07/12 职场文书
现场活动策划方案
2014/08/22 职场文书
2014工程部年度工作总结
2014/12/17 职场文书
明确岗位职责
2015/02/14 职场文书
大学生军训心得体会5篇
2019/08/15 职场文书
MySQL 原理与优化之Update 优化
2022/08/14 MySQL