詳解Nodejs get獲取遠程服務器接口數(shù)據(jù)
本文實例為大家分享了Nodejs get獲取遠程服務器接口數(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.路由端調用: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'); /** * 靜態(tài)文件目錄 */ app.use(express.static('public')); /** * 加載路由配置 */ routes(app,__dirname); /** * 啟動服務器 */ http.createServer(app).listen(app.get('port'), function(){ console.log("服務器已經(jīng)啟動了" + 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 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>
以上所述是小編給大家介紹的Nodejs get獲取遠程服務器接口數(shù)據(jù)詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
npm?does?not?support?Node.js問題的解決辦法
這篇文章主要給大家介紹了關于npm?does?not?support?Node.js問題的解決辦法,文中通過代碼以及圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-10-10Node.js多文件Stream合并,串行和并發(fā)兩種模式的實現(xiàn)方式
這篇文章主要介紹了Node.js多文件Stream合并,串行和并發(fā)兩種模式的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10Windows 系統(tǒng)下設置Nodejs NPM全局路徑
這篇文章主要介紹了Windows 系統(tǒng)下設置Nodejs NPM全局路徑2016-04-04Node.js實現(xiàn)爬取網(wǎng)站圖片的示例代碼
本文將利用Node.js開發(fā)一個小示例—爬取某圖片網(wǎng)站的圖片,文中涉及的知識點有https模塊、cheerio模塊、fs模塊和閉包,感興趣的可以了解一下2022-04-04electron demo項目npm install安裝失敗的解決方法
下面小編就為大家分享一篇electron demo項目npm install安裝失敗的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02快速搭建Node.js(Express)用戶注冊、登錄以及授權的方法
這篇文章主要介紹了快速搭建Node.js(Express)用戶注冊、登錄以及授權,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05NestJS開發(fā)核心概念Providers類基本用法詳解
這篇文章主要為大家介紹了NestJS開發(fā)核心概念Providers類基本用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08