如何使用https模块读取Nodejs中的POST图像数据



如果不使用Node.jsexpress模块,我可以使用https模块读取两个POST数据变量吗?

我的代码运行良好,只是我需要从请求中获得两个POST请求值(许可证和照片(。如何从POST请求中提取两个部分?

const https = require('https');
const fs = require('fs');
const options = {
secure: true,
key: fs.readFileSync('ssl/key.pem'),
cert: fs.readFileSync('ssl/public.pem'),
ca: fs.readFileSync('ssl/cap1_led_com.ca-bundle')
};
https.createServer(options, function (req, res)
{
let body = '';
if (req.method === 'GET' && req.url === '/')
{
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('./http-form.html', 'UTF-8', (err, data) => {
if (err)
throw err;
res.write(data);
res.end();
});
}
else if (req.method === 'POST')
{
req.on('data', (data) => {
body += data;
});
req.on('end', () => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(body, () => {
res.end();
});
});
}
else
{
res.writeHead(404, {'Content-Type': 'text/html'});
res.end(`<h1>404 ERROR could not find that Page</h1>`);
}
}).listen(443);

有人在这里发布了答案,但后来意外地删除了它,这实际上起了作用:

const https = require('https');
const fs = require('fs');
const tmp = require('tmp');
const options = {
secure: true,
key: fs.readFileSync('ssl/key.pem'),
cert: fs.readFileSync('ssl/public.pem'),
ca: fs.readFileSync('ssl/capt1_tf.ca-bundle')
};
https.createServer(options, function (req, res)
{
let body = '';
if (req.method === 'GET' && req.url === '/')
{
res.writeHead(200, {'Content-Type': 'text/html'});
fs.readFile('./http-form.html', 'UTF-8', (err, data) => {
if (err)
throw err;
res.write(data);
res.end();
});
}
else if (req.method === 'POST')
{
req.on('data', (data) => {
body += data;
});
req.on('end', () => {
const map = {};
body.split('&')
.map((pair) => pair.split('='))
.forEach((splitPair) => {
map[splitPair[0]] = splitPair[1];
});
const decodeUriComponent = require('decode-uri-component');

tmp.file({ discardDescriptor: true },function _tempFileCreated(err, path, fd, cleanupCallback) {
if (err) throw err;
console.log(path);
fs.writeFile(path, decodeUriComponent(map.base64), 'base64', function(err) {
});
});

});
}
else
{
res.writeHead(404, {'Content-Type': 'text/html'});
res.end(`<h1>404 ERROR could not find that Page</h1>`);
}
}).listen(443);

相关内容

  • 没有找到相关文章

最新更新