詳解Nodejs get獲取遠(yuǎn)程服務(wù)器接口數(shù)據(jù)
本文實(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 = {
/**
* 測(cè)試獲取所有的區(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ù)啟動(dòng)入口:
/**
* 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);
/**
* 啟動(dòng)服務(wù)器
*/
http.createServer(app).listen(app.get('port'), function(){
console.log("服務(wù)器已經(jīng)啟動(dò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)用頁(yè)面 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">返回首頁(yè)</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獲取遠(yuǎn)程服務(wù)器接口數(shù)據(jù)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- node.js處理前端提交的GET請(qǐng)求
- Node.js系列之發(fā)起get/post請(qǐng)求(2)
- nodejs 使用http進(jìn)行post或get請(qǐng)求的實(shí)例(攜帶cookie)
- nodejs之koa2請(qǐng)求示例(GET,POST)
- nodejs使用http模塊發(fā)送get與post請(qǐng)求的方法示例
- node.js中的http.get方法使用說(shuō)明
- node.js中的http.response.getHeader方法使用說(shuō)明
- 如何在node環(huán)境實(shí)現(xiàn)“get數(shù)據(jù)解析”代碼實(shí)例
相關(guān)文章
利用nodeJs anywhere搭建本地服務(wù)器環(huán)境的方法
今天小編就為大家分享一篇利用nodeJs anywhere搭建本地服務(wù)器環(huán)境的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Nodejs Post請(qǐng)求報(bào)socket hang up錯(cuò)誤的解決辦法
這篇文章主要介紹了Nodejs Post請(qǐng)求報(bào)socket hang up錯(cuò)誤的解決辦法,本文因少加了headers字段信息導(dǎo)致出現(xiàn)這個(gè)錯(cuò)誤,本文給出了一個(gè)完整的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-09-09
nodejs中向HTTP響應(yīng)傳送進(jìn)程的輸出
這篇文章主要介紹了nodejs中向HTTP響應(yīng)傳送進(jìn)程的輸出 ,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
node+axios實(shí)現(xiàn)下載外網(wǎng)文件到本地
這篇文章主要為大家介紹了node+axios實(shí)現(xiàn)下載外網(wǎng)文件到本地示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
基于html5和nodejs相結(jié)合實(shí)現(xiàn)websocket即使通訊
HTML5 擁有許多引人注目的新特性,如 Canvas、本地存儲(chǔ)、多媒體編程接口、WebSocket 等等。雖然現(xiàn)在大家把它捧的很火的樣子,但是個(gè)人認(rèn)為它還需要其他平臺(tái)的支持才能真正的"火起來(lái)"2015-11-11
淺談Node.js輕量級(jí)Web框架Express4.x使用指南
本篇文章主要介紹了淺談Node.js輕量級(jí)Web框架Express4.x使用指南,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
nodejs入門教程五:連接數(shù)據(jù)庫(kù)的方法分析
這篇文章主要介紹了nodejs入門教程之連接數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了nodejs連接數(shù)據(jù)庫(kù)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-04-04

