• nodejs——向另一个服务器发送文件


    业务需要,需在客户本地建立一个服务,将本地的数据库文件发送到主服务器。

    下面是本地服务代码:

    var exec = require('child_process').exec;
    var request = require('request');
    var fs = require('fs');
    
    exec('./mysqlout.bat', function (err, stdout, stderr) {
    
        if (err) {
            console.error(err);
            return;
        }
        request.post('http://120.11.xx.19:3000/upload', {
            formData: {
                title: 'upload sqlfile',
                description: 'Sent on ' + new Date(),
                is_public: 1,
                sqlfiles: fs.createReadStream('f:/beijian.sql')
            },
            json: true
        }, function(err, res, body) {
            console.log('返回: ' + body);
            return;
        })
    });

    远程端主服务代码:

    //express框架
    
    app.js:
    //引入copyDB文件
    var copyDB = require('./routes/copyDB');
    //新建路由
    app.use('/upload', copyDB);
    
    
    codyDB.js:
    var express = require('express');
    var router = express.Router();
    var debug = require('debug')('Web:server');
    var MULTIPARTY = require('multiparty');
    var exec = require('child_process').exec;
    var FS = require('fs');
    var CONFIG = require('../config');
    
    var copyDB = function(req, res, next) { 
      //接收文件的方式用multiparty包
      var form = new MULTIPARTY.Form({
        uploadDir: "./appData/sqlFile/"// 接收文件后存储的路径,文件名是一串字符串,下面用fs.rename方法改名
      });
      form.parse(req, function(err, fields, files) {
        if (err) {
          debug("upload failed!" + err);
          res.send('upload failed!');
          return;
        } else {
          debug('upload success!');
          res.send('upload success!');
          var inputFile = files.sqlfiles[0];
          var uploadedPath = inputFile.path;
          var dstPath = './appData/sqlFile/' + 'new' + '.sql';
          FS.rename(uploadedPath, dstPath, function(renameerr) {
            if (renameerr) {
              debug('rename error: ' + err);
              return;
            } else {
              debug('rename ok');
            }
          })
        }
      });
    
    };
    
    router.post('/', copyDB);
    
    module.exports = router;
  • 相关阅读:
    (原创)xcode4的workspace里各lib工程与app工程联编之runscript简介
    使用textmate
    (转)DebuggingTechniques
    (转)ObjectiveC的单例模式(singleton)
    VIA = Via Inner Action
    Das Vergessmichnicht
    Resume
    Explore Subdivide Surface Algorithm Of Maya
    为什么我的文章总是没人回复
    Summer Dream Für Meines Leben
  • 原文地址:https://www.cnblogs.com/yourstars/p/6728765.html
Copyright © 2020-2023  润新知