Как читать данные изображения POST в Nodejs с помощью модуля https - PullRequest
0 голосов
/ 07 августа 2020

Без использования модуля Node.js express, могу ли я прочитать две переменные данных 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);

1 Ответ

0 голосов
/ 08 августа 2020

Кто-то, кто разместил здесь ответ, но неожиданно удалил его позже, что действительно сработало:

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);
...