服務器請求百度首頁
- 參考文檔鏈接
//服務器發送請求
const http = require('http');
const path = require('path');
const fs = require('fs');
let options = {
hostname: 'www.baidu.com',
port: '80'
}
let req = http.request(options, (res) => {
let info = '';
res.on('data', (chunk) => {
info += chunk;
})
res.on('end', () => {
fs.writeFile(path.join(__dirname, 'baidu.html'), info, (err) => {
if (!err) {
console.log("百度主頁html內容加載完畢")
}
})
})
})
req.end();
服務器向後臺接口發送請求
- 查詢數據
const http = require('http');
let options = {
protocol: 'http:',
hostname: 'localhost',
port: '3000',
path: '/books'
}
let req = http.request(options, (res) => {
let info = '';
res.on('data', (chunk) => {
info += chunk;
})
res.on('end', () => {
console.log(info);
})
})
req.end();
- 添加數據
- 需要在headers中指定Content-Type
- 需要引入querystring模塊,對請求的參數對象轉成字符串進行處理
- 需要調用req的write(data)方法將請求的參數寫入請求體中
//服務器向添加圖書resful接口發送帶參請求
const http = require('http');
const querystring = require('querystring');
let options = {
protocol: 'http:',
hostname: 'localhost',
port: '3000',
path: '/books/book',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
//對應添加數據的字段和值
let addData = querystring.stringify({
'name': '書名',
'author': '作者',
'category': '分類',
'description': '描述'
});
let req = http.request(options, (res) => {
let info = '';
res.on('data', (chunk) => {
info += chunk;
})
res.on('end', () => {
console.log(info); //打印接口返回的信息
})
})
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.write(addData);
req.end();
- 帶參查詢數據
- 在請求路徑後拼接參數Id(請求的後臺接口是resful接口)
//帶參查詢
const http = require('http');
let id = 9;
let options = {
protocol: 'http:',
hostname: 'localhost',
port: '3000',
path: '/books/book/' + id,
method: 'GET'
};
let req = http.request(options, (res) => {
let info = '';
res.on('data', (chunk) => {
info += chunk;
})
res.on('end', () => {
console.log(info);
})
})
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.end();
- 修改數據
- 與添加數據基本一致,只需要更改請求方式爲put以及參數中添加修改的數據id
let options = {
protocol: 'http:',
hostname: 'localhost',
port: '3000',
path: '/books/book',
method: 'PUT',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
//對應添加數據的字段和值
let addData = querystring.stringify({
'id': 18,
'name': '書名修改',
'author': '作者修改',
'category': '分類修改',
'description': '描述修改'
});
- 刪除數據
- 刪除數據與帶參查詢數據一致,只需要更改請求方式爲delete
let id = 18;
let options = {
protocol: 'http:',
hostname: 'localhost',
port: '3000',
path: '/books/book/' + id,
method: 'delete'
};
請求第三方服務器的後臺接口
封裝天氣查詢模塊
//weather.js
const http = require('http');
//http://www.weather.com.cn/data/sk/101010100.html 通過拼接城區Id查詢
exports.queryWeather = (cityCode, callback) => {
let options = {
protocol: 'http:',
hostname: 'www.weather.com.cn',
port: '80',
path: '/data/sk/' + cityCode + '.html',
method: 'get'
};
let req = http.request(options, (res) => {
let info = '';
res.on('data', (chunk) => {
info += chunk;
})
res.on('end', () => {
info = JSON.parse(info);
callback(info);
})
})
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.end();
}
測試封裝模塊
//test.js
const weather = require('./weather.js')
weather.queryWeather('101020100', (data) => {
console.log(data); //打印出對應ID的天氣信息
})