【问题标题】:Getting error: cannot read property 'path' of undefined出现错误:无法读取未定义的属性“路径”
【发布时间】:2022-01-06 14:43:26
【问题描述】:

我需要将 List<Map<String, String>> 作为 REST GET API 的参数传递。我需要知道如何从 Postman 或类似工具传递它。

我尝试将其设置为 GET API 的 BODY,但它给了我错误。

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');

http.createServer(function(req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
      var oldpath = files.filetoupload.path;
      var newpath = 'D:/nodejs/images/' + files.filetoupload.name;
      fs.rename(oldpath, newpath, function(err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        res.end();
      });
    });
  } else {
    res.writeHead(200, {
      'Content-Type': 'text/html'
    });
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
}).listen(3000);

【问题讨论】:

标签: javascript node.js express


【解决方案1】:

GET 请求消息中的有效负载没有定义的语义;在 GET 请求上发送有效负载正文可能会导致某些现有实现拒绝该请求。 我建议使用 POST 方法而不是 GET

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2014-07-28
    • 2018-08-18
    • 2015-11-22
    • 2014-08-16
    相关资源
    最近更新 更多