欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-17 09:01:46
本文实例为大家分享了nodejs get获取远程服务器接口数据的具体代码,供大家参考,具体内容如下 1.get模块:_get.js /** * crea...

本文实例为大家分享了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获取远程服务器接口数据详解整合,希望对大家有所帮助