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

下面是本地服务代码:

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;

 

posted on 2017-04-18 17:06  Boom__Clap  阅读(3837)  评论(0编辑  收藏  举报