这个 REST API 代码有什么问题?

What is wrong with this REST API code?

我在使用此代码时遇到的错误是

Cannot POST /img/upload/'

代码:

 app.post('/img/upload/',[multer({ dest: __dirname+'/www/images/new/',
            rename: function (fieldname, filename, req, res) {
                    return filename + '_ORIGINAL';
            },
            onFileUploadStart: function (file, req, res) {
                     console.log(file.fieldname + ' is starting ...')
            },
            onFileUploadData: function (file, data, req, res) {
                     console.log(data.length + ' of ' + file.fieldname + ' arrived')
            },
            onFileUploadComplete: function (file, req, res) {
                     console.log(file.fieldname + ' uploaded to  ' + file.path)
            },
            onError: function (error, next) {
                     console.log(error)
                     next(error)
            }
    })]);

对此 API 的控制器请求是:

    var data = new FormData;
    data.append("file", files[0]);

     $http({
            url: 'http://52.25.181.109/img/upload/',
            method: "POST",
            data : data,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function (data, status, headers, config) {
            $scope.hide_u();
            console.log(data);
        }).error(function (data, status, headers, config) {
            $scope.hide_u();
            console.log(data);
        });

代码执行错误。我该如何纠正?

尝试更改您的 header:headers: {'Content-Type': 'multipart/form-data'} Multer 仅满足多部分请求。该路线对 "application/x-www-form-urlencoded" 无效。形成他们的网站:

Node.js middleware for handling multipart/form-data.

更新: 我架设了一个服务器。以下应该有效,更改服务器处理程序:

app.use(multer({ dest: __dirname+'/www/images/new/'}));
app.post("/img/upload/", function() {console.log("request rec.")});

我的测试表格是:"<form enctype=\"multipart/form-data\">"

建议:尝试删除 header。