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.项目目录如下:
这里写图片描述
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="stylesheet">
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" 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="/" 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>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值