简体   繁体   English

将图像上传到其他域nodejs

[英]Upload images to a different domain nodejs

Background 背景

I tried to use angular-file-upload module to upload the images from localhost:3000 to localhost:9000 which I assume they are different domain and should belongs to CORS (Cross-origin resource sharing). 我尝试使用angular-file-upload模块将图像从localhost:3000上传到localhost:9000,我认为它们是不同的域,并且应属于CORS (跨域资源共享)。 I see this module supports CORS . 我看到此模块支持CORS I also follow the express server setup that they recommend here . 我还遵循他们在这里推荐的快速服务器设置。 but still I can not see anything in either the body object or files object in the request. 但我仍然无法在请求的主体对象或文件对象中看到任何内容。

Questions 问题

  1. Since this module supports CORS, why seems still not work. 由于此模块支持CORS,因此为什么似乎仍然无法使用。
  2. Should I explicitly tell node server to set up something in the responds header like (Access-Control-Allow-Methods, Access-Control-Allow-Origin, Access-Control-Allow-Headers) and how to do that in express ? 我是否应该明确地告诉节点服务器在响应标头中设置某些内容(例如,Access-Control-Allow-Methods,Access-Control-Allow-Origin,Access-Control-Allow-Headers),以及如何做到这一点? res.setHeader(.....) ? res.setHeader(.....)吗?
  3. How can I fix this problem? 我该如何解决这个问题?

Code

/* ***************************** ANGULAR ***************************************** */
var myApp = angular.module('myApp', ['angularFileUpload']);
myApp.controller('MyCtrl', [ '$scope', '$upload', function($scope, $upload) {
  $scope.$watch('files', function(files) {
    if (files) {
        for (var i = 0; i < $scope.files.length; i++) {
          var file = $scope.files[i];
          $scope.upload = $upload.upload({
            url: 'http://localhost:9000/upload/',
            data: {myObj: $scope.myModelObj},
            file: file,
          }).progress(function(evt) {
            console.log('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :'+ evt.config.file.name);
          }).success(function(data, status, headers, config) {
            console.log('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
          });
        }
    }
  });
}]);

/* ***************************** SERVER SIDE ***************************************** */
var multipart = require('connect-multiparty');
var express = require('express');
var bodyParser = require('body-parser')
var _ = require('underscore');
var path = require('path');
var app = express();

app.use(multipart({
    uploadDir: './uploads'
}));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

app.use(function (req, res, next) {
    console.log(req.files); // {} why ????
    console.log(req.body); // {} why ????
});

app.listen(9000);

Actually, I figure it out by using a middleware called cors . 实际上,我通过使用名为cors的中间件弄清楚了 so the server code looks like this 所以服务器代码看起来像这样

var multipart = require('connect-multiparty');
var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser')
var app = express();

app.use(multipart({
    uploadDir: './uploads'
}));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

// cors middleware
app.use(cors());

app.use(function (req, res, next) {
    console.log(req.files); // then there is something : )
    next();
});

app.listen(9000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM