从AngularJS发布到Node.js发布函数

时间:2017-07-14 12:07:10

标签: angularjs node.js servlets post

我在前端(AngularJS)发布到我的后端(Node.js)中的函数时遇到问题。

我一直收到404错误。有人可以看看我的代码是否有问题,因为我无法看到可能导致此问题的原因。

前端

MockUpMaker_v1 / JS / controller.js

// All photos've been pushed now sending it to back end

    $timeout(function () {
        $http.post('/MockUpMaker_v1/savePhotos', $scope.photosToPhp).then(function (success) {
            $scope.generating = false;
            $scope.generateBtn = 'Generate';
            //creating mock up gallery
            for (var x = 0; x < success.data.photos; x++) {
                var file = '/MockUpMaker_v1/tmp/' + success.data.folder + "/out" + x + ".png";
                $scope.gallery.push(file);
            }
            $scope.photosToPhp = [];
        }, function (error) {
    });

},

后端

MockUpMaker_v1 / server.js

app.post('/MockUpMaker_v1/savePhotos', function(req, res){

    var folder = Math.random().toString(36).substr(2, 20);
    var photos = req.body;
    var counts = 0;
    var callback = function(counts){
      if(counts < photos.length){
        saveBase64(photos[counts], folder, counts, callback);
      }else{
        // var counts = 0;
        var response = {"folder": folder, "photos": photos.length};
        res.send(response)
      }
    };
    saveBase64(photos[counts], folder, counts, callback);
});

完整后端

根据要求

"use strict";
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var mkdirp = require('mkdirp');
var archiver = require('archiver');
var app = express();
var server = require('http').Server(app);

app.use(bodyParser.json({limit: '50mb'}));

app.use(express.static('/public'));
app.use(express.static('/js'));
app.use(express.static('/tmp'));
app.use(express.static('/img'));
app.use(express.static('/css'));

app.get('/', function(req, res){
  res.sendFile(__dirname + 'views/form-mockup.html')
});


app.post('/MockUpMaker_v1/savePhotos', function(req, res){

  var folder = Math.random().toString(36).substr(2, 20);
  var photos = req.body;
  var counts = 0;
  var callback = function(counts){
    if(counts < photos.length){
      saveBase64(photos[counts], folder, counts, callback);
    }else{
      // var counts = 0;
      var response = {"folder": folder, "photos": photos.length};
      res.send(response)
    }
  };
  saveBase64(photos[counts], folder, counts, callback);
});

app.post('MockUpMaker_v1/downloadZip', function(req, res){
  var photos = req.body;
  var out =  photos[0];
  var test = out.split('/');
  var loc  = test.pop();
  var end =  test.join('/');
    console.log(end);

  var outName = '/' + end + '/MockUp.zip';
  var output = fs.createWriteStream(outName);
  var archive = archiver('zip', {store: true });
  var zip = function(photos, f){
    for(var t = 0; t < photos.length; t++){
      var file = 'mockUp' + t + '.jpg';
      var from = '/' +  photos[t];
      archive.file( from, { name: file });
    }
    f();
  };


  output.on('close', function() {
    var photos = req.body;
    var out =  photos[0];
    var test = out.split('/');
    var loc  = test.pop();
    var end =  test.join('/');
    res.send(end + '/MockUp.zip');

    console.log('archiver has been finalized and the output file descriptor has closed.');
  });
  archive.on('error', function(err) {
    throw err;
  });

  archive.pipe(output);
  zip(photos, f);
  function f(){
    archive.finalize();
  }
});

server.listen(3000, function(){
  console.log('sm2.0 server running');
});

function saveBase64(photo, folder, counts, callback){
  var result = photo.split(',')[1];
  var path = 'tmp/' + folder;
  var filename = path + "/out" + counts + ".png";
  mkdirp( path, function() {

    fs.writeFile(filename, result, 'base64', function(error){
      if (error) {
        console.log('error saving photo');
      }else{
        console.log('photo saved');
        counts++;
        callback(counts);
    }
    });
  });
}

和控制台打印输出

  

[nodemon]由于更改而重新启动... [nodemon]因重启而重启   更改... [nodemon]启动node server.js sm2.0服务器正在运行

检查错误

POST http://localhost:63342/MockUpMaker_v1/savePhotos 404 (Not Found)

1 个答案:

答案 0 :(得分:0)

在Angular App中,将整个URL写为$ http.post函数的第一个参数。

E.g。 http://localhost:3000/MockUpMaker_v1/savePhotos