app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
POST /test HTTP/1.1 Host: 127.0.0.1:3000 Content-Type: text/plain; charset=utf8 Content-Encoding: gzip chyingp
var http = require('http');
var options = {
hostname: '127.0.0.1',
port: '3000',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Content-Encoding': 'identity'
}
};
var client = http.request(options, (res) => {
res.pipe(process.stdout);
});
client.end('chyingp');
var http = require('http');
var parsePostBody = function (req, done) {
var arr = [];
var chunks;
req.on('data', buff => {
arr.push(buff);
});
req.on('end', () => {
chunks = Buffer.concat(arr);
done(chunks);
});
};
var server = http.createServer(function (req, res) {
parsePostBody(req, (chunks) => {
var body = chunks.toString();
res.end(`Your nick is ${body}`)
});
});
server.listen(3000);
var http = require('http');
var querystring = require('querystring');
var options = {
hostname: '127.0.0.1',
port: '3000',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Encoding': 'identity'
}
};
var jsonBody = {
nick: 'chyingp'
};
var client = http.request(options, (res) => {
res.pipe(process.stdout);
});
client.end( JSON.stringify(jsonBody) );
var http = require('http');
var parsePostBody = function (req, done) {
var length = req.headers['content-length'] - 0;
var arr = [];
var chunks;
req.on('data', buff => {
arr.push(buff);
});
req.on('end', () => {
chunks = Buffer.concat(arr);
done(chunks);
});
};
var server = http.createServer(function (req, res) {
parsePostBody(req, (chunks) => {
var json = JSON.parse( chunks.toString() ); // 关键代码
res.end(`Your nick is ${json.nick}`)
});
});
server.listen(3000);
var http = require('http');
var querystring = require('querystring');
var options = {
hostname: '127.0.0.1',
port: '3000',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'form/x-www-form-urlencoded',
'Content-Encoding': 'identity'
}
};
var postBody = { nick: 'chyingp' };
var client = http.request(options, (res) => {
res.pipe(process.stdout);
});
client.end( querystring.stringify(postBody) );
var http = require('http');
var querystring = require('querystring');
var parsePostBody = function (req, done) {
var length = req.headers['content-length'] - 0;
var arr = [];
var chunks;
req.on('data', buff => {
arr.push(buff);
});
req.on('end', () => {
chunks = Buffer.concat(arr);
done(chunks);
});
};
var server = http.createServer(function (req, res) {
parsePostBody(req, (chunks) => {
var body = querystring.parse( chunks.toString() ); // 关键代码
res.end(`Your nick is ${body.nick}`)
});
});
server.listen(3000);
var http = require('http');
var iconv = require('iconv-lite');
var encoding = 'gbk'; // 请求编码
var options = {
hostname: '127.0.0.1',
port: '3000',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'text/plain; charset=' + encoding,
'Content-Encoding': 'identity',
}
};
// 备注:nodejs本身不支持gbk编码,所以请求发送前,需要先进行编码
var buff = iconv.encode('程序猿小卡', encoding);
var client = http.request(options, (res) => {
res.pipe(process.stdout);
});
client.end(buff, encoding);
var http = require('http');
var contentType = require('content-type');
var iconv = require('iconv-lite');
var parsePostBody = function (req, done) {
var obj = contentType.parse(req.headers['content-type']);
var charset = obj.parameters.charset; // 编码判断:这里获取到的值是 'gbk'
var arr = [];
var chunks;
req.on('data', buff => {
arr.push(buff);
});
req.on('end', () => {
chunks = Buffer.concat(arr);
var body = iconv.decode(chunks, charset); // 解码操作
done(body);
});
};
var server = http.createServer(function (req, res) {
parsePostBody(req, (body) => {
res.end(`Your nick is ${body}`)
});
});
server.listen(3000);
var http = require('http');
var zlib = require('zlib');
var options = {
hostname: '127.0.0.1',
port: '3000',
path: '/test',
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip'
}
};
var client = http.request(options, (res) => {
res.pipe(process.stdout);
});
// 注意:将 Content-Encoding 设置为 gzip 的同时,发送给服务端的数据也应该先进行gzip
var buff = zlib.gzipSync('chyingp');
client.end(buff);
var http = require('http');
var zlib = require('zlib');
var parsePostBody = function (req, done) {
var length = req.headers['content-length'] - 0;
var contentEncoding = req.headers['content-encoding'];
var stream = req;
// 关键代码如下
if(contentEncoding === 'gzip') {
stream = zlib.createGunzip();
req.pipe(stream);
}
var arr = [];
var chunks;
stream.on('data', buff => {
arr.push(buff);
});
stream.on('end', () => {
chunks = Buffer.concat(arr);
done(chunks);
});
stream.on('error', error => console.error(error.message));
};
var server = http.createServer(function (req, res) {
parsePostBody(req, (chunks) => {
var body = chunks.toString();
res.end(`Your nick is ${body}`)
});
});
server.listen(3000);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有