国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

詳解Nodejs get獲取遠(yuǎn)程服務(wù)器接口數(shù)據(jù)

2019-11-02 14:13:25
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Nodejs get獲取遠(yuǎn)程服務(wù)器接口數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下

1.GET模塊:_get.js

/** * Created by jinx on 7/7/17. */var http = require('http');module.exports = {  /**  * 測試獲取所有的區(qū)域  * /  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.路由端調(diào)用: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.服務(wù)啟動入口:

/** * 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');/** * 靜態(tài)文件目錄 */app.use(express.static('public'));/** * 加載路由配置 */routes(app,__dirname);/** * 啟動服務(wù)器 */http.createServer(app).listen(app.get('port'), function(){ console.log("服務(wù)器已經(jīng)啟動了" + app.get('port'));});

4.項(xiàng)目目錄如下:

5.調(diào)用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.調(diào)用頁面 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  rel="external nofollow" rel="stylesheet">  <link  rel="external nofollow" rel="stylesheet">  <link  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>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 漳州市| 蒙山县| 浪卡子县| 寿阳县| 精河县| 镇康县| 怀集县| 常山县| 上高县| 利川市| 剑河县| 称多县| 无极县| 赤城县| 贵州省| 正蓝旗| 新平| 新宁县| 金华市| 蓝田县| 弋阳县| 武威市| 连江县| 仁怀市| 镇雄县| 云霄县| 林甸县| 九龙坡区| 铜山县| 金沙县| 中山市| 龙门县| 禹城市| 阳谷县| 墨江| 兴化市| 武汉市| 方正县| 定远县| 锡林郭勒盟| 平谷区|