将文件从一台服务器发送到另一台

时间:2019-03-24 10:52:31

标签: node.js express multer

我正在制作一个使用multipart / form-data将文件上传到服务器的应用程序。要在Node js中接收Multipart / form-data请求,我正在使用multer并将该文件存储到磁盘存储中。然后,我将这些文件分成三部分。

现在,我希望服务器将这些部件发送到多个服务器。


const express = require('express');

const path = require('path');

const multer = require('multer');

const methodOverride = require('method-override');

const bodyParser = require('body-parser');

const PORT = 4000;

const crypto = require('crypto');

const fs = require('fs');

const splitFile = require('split-file');

const app = express();

app.use(bodyParser.json());
app.use(methodOverride('_method'));


var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, upload_path)
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
})


var upload = multer({ storage: storage });

app.post('/upload', upload.single('file'), (req, res) => {

  res.redirect('localhost:3000/');
})

app.post('/encrypt/:id', (req, res) => {
  console.log(req.params.id);

  console.log(enc_path + '\\' + req.params.id);

  var key = 
 '14189dc35ae35e75ff31d7502e245cd9bc7803838fbfd5c773cdcd79b8a28bbd';
  var cipher = crypto.createCipher('aes-256-cbc', key);
  var input = fs.createReadStream(upload_path + '//' + req.params.id);
  var output = fs.createWriteStream(enc_path + '//' + req.params.id);
  input.pipe(cipher).pipe(output);

  output.on('finish', function () {
    console.log('Encrypted file written to disk!');
  });


})

app.post('/split/:id', (req, res) => {
  console.log(req.params.id);

  splitFile.splitFile(enc_path + '\\' + req.params.id, 3).then((names) => {


  })

})

app.listen(PORT, function () {
  console.log("Server is running on Port " + PORT);
});

0 个答案:

没有答案