0

在不使用 Node.jsexpress模块的情况下,我可以使用模块读取两个 POST 数据变量https吗?

我的代码工作正常,只是我需要从请求中获取两个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);
4

1 回答 1

0

有人在这里发布了答案,但后来令人惊讶地删除了它,这实际上有效:

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);
于 2020-08-08T16:51:54.730 回答