温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-03 03:10:22 来源:脚本之家 阅读:224 作者:Jinx_Q 栏目:web开发

本文实例为大家分享了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获取远程服务器接口数据详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI