无法在Node JS中使用Request或Axios发布文件

时间:2020-07-24 19:23:44

标签: javascript node.js npm request axios

我必须从前端代码中的表单中获取数据,然后使用API​​调用将数据上传到另一台服务器,目前无法执行此操作。我尝试了互联网上提到的多种方法和技巧,以解决在使用Request和Axios在NodeJ中上传文件时遇到的问题,但是它们都不适用于这种特殊情况,我不知道为什么。

如果使用Axios,这是我客户端的代码:

import pandas as pd

df1 = pd.DataFrame(data={
    'date':['2020070{}'.format(i) for i in [5,6,7,7,8,8]],
    'column1':['a', 'b','f','g','a','g'],
    'column2':['{}%'.format(i) for i in [1.1,78,10,59,69,12]]
})

df2 = pd.DataFrame(data={
    'date':['2020070{}'.format(i) for i in [7,7,8,8]],
    'column1':['q','w','e','r'],
    'column2':['{}%'.format(i) for i in [11,54,64, 11]]
})

dates_not_in_2 = [x for x in df1['date'] if x not in df2['date'].tolist()]
dates_common = [x for x in df1['date'] if x in df2['date'].tolist()]

combined = pd.concat([df1.loc[df1.date.isin(dates_not_in_2)], df2.loc[df2.date.isin(dates_common)]], axis=0).reset_index(drop=True)

我面临的错误

app.post('/job/addjob', upload.any(), (req, res) => {
    console.log(req.files[0].path);
    var form = new formData();
    form.append('Title', req.body.Title);
    // form.append('Services', JSON.stringify(req.body.Services));
    form.append('Startdate', req.body.Startdate);
    form.append('NoOfPositions', req.body.NoOfPositions);
    form.append('EndDate', req.body.EndDate);
    form.append('Experience', req.body.Experience);
    form.append('Rate', req.body.Rate);
    form.append('Location', req.body.Location);
    form.append('Description', req.body.Description);
    // form.append('Questions', JSON.stringify(req.body.Questions));
    form.append('Dresscode', fs.createReadStream(req.files[0].path));
    axios({
        method: 'post',
        url: 'http://13.127.239.92:3000/job/addjob',
        data: form,
        headers: {'Content-Type': 'multipart/form-data', 'Authorization': req.session.verifiedToken}
    }).then(function (response) {
        console.log(response.data);
        res.redirect('/provider');
    })
    .catch(function (err) {
        //handle error
        console.log(err);
        res.redirect('/profile/getProfile');
    });
});

如果使用Request,这是客户端的代码:

Error Uploading Image.

我遇到的错误是:

app.post('/job/addjob', upload.any(), (req, res) => {
    console.log(req.files);
    var options = {
      'method': 'POST',
      'url': 'http://13.127.239.92:3000/job/addJob',
      'headers': {
        'Content-Type': 'multipart/form-data',
        'Authorization': req.session.verifiedToken
      },
      formData: {
        'Title': req.body.Title,
    // form.append('Services', JSON.stringify(req.body.Services));
        'Startdate': req.body.Startdate,
        'NoOfPositions': req.body.NoOfPositions,
        'EndDate': req.body.EndDate,
        'Experience': req.body.Experience,
        'Rate': req.body.Rate,
        'Location': req.body.Location,
        'Description': req.body.Description,
    // form.append('Questions', JSON.stringify(req.body.Questions));
        'Dresscode1': fs.createReadStream(req.files[0].path),
      }
    };
    
    request(options, function (error, response) {
        if (error) { console.log(error) }
        else {
            console.log(response.data);
            res.redirect('/provider');
        }
    });
});

这是服务器端的一部分代码,可能有助于理解我的问题:

{ Error: socket hang up
    at createHangUpError (_http_client.js:323:15)
    at Socket.socketOnEnd (_http_client.js:426:23)
    at Socket.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }

最糟糕的是,我只能更改客户端代码,而不能更改服务器端代码

0 个答案:

没有答案