//服务器发送请求
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();
//服务器向添加图书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();
//带参查询
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();
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': '描述修改'
});
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的天气信息
})