为我的Node.JS应用程序创建可执行文件

时间:2018-02-20 17:30:09

标签: node.js

标题说明了一切。我正在尝试使用npm pkgdetails here)创建Windows exe文件。我已经成功创建了exe文件,但它只在位于我的项目文件夹中时才会运行。我希望能够从任何地方运行它 - 甚至是其他计算机。我做错了什么?

编辑:这是我的剧本。使用_dirname会有帮助吗?我不熟悉这个函数,所以代码示例将不胜感激。

var AWS = require('aws-sdk'),
    fs = require('fs');
    const Fs = require('fs')
const path = require('path');
AWS.config.loadFromPath(path.join(__dirname, 'config.json'));
var mysql      = require('mysql');
var connection = mysql.createConnection({
  connectionLimit : 10,
  host     : '...',
  user     : '...',
  password : '...',
  database: '...',
});
console.log(__dirname);

var localfile = path.join(__dirname, 'myverse.mp3')

 connection.query('SELECT COUNT(mp3) as "total" FROM myverses where mp3 = "empty"', function (error, results, fields) {
  console.log('error: ' + error);
  var totalverses = results[0].total

  if (totalverses > 0) {
    console.log('There are ' + results[0].total + ' verses to record.');
    addverse();
      } else {
    console.log("There are NO verses to record.")
    connection.end();
  }

  });

  function addanother() {
    connection.query('SELECT COUNT(mp3) as "total" FROM myverses where mp3 = "empty"', function (error, results, fields) {
   console.log('error: ' + error);
   var totalverses = results[0].total


   if (totalverses > 0) {
     console.log('There are ' + results[0].total + ' verses to record.');
     addverse();
       } else {
     console.log("There are NO verses to record.")
    connection.end();
   }

   });
}


function addverse() {
    connection.query('SELECT versetext, book, mp3, id, reference FROM myverses where mp3 = "empty" limit 1',
    function (error, results, fields) {
    console.log(error);
    var scripture = results[0].versetext;
    var book = results[0].book;
    var reference = results[0].reference.replace(":", " verse ");
    console.log(scripture + " " +  book.replace("1", "first").replace("2", "second").replace("3", "third") + " " + reference);
    var myverse = "<speak><break time='1s'/>" + scripture + " " +  book.replace("1", "first").replace("2", "second").replace("3", "third") + " " + reference + "<break time='1s'/></speak>";
    var link = "https://s3.amazonaws.com/myverses/" + book.replace(/ /g, "")+reference.replace(/ /g, "")+".mp3";

    writeit();
    var myvalue = fs.createReadStream(localfile);
    setTimeout(uploadit, 2000)

function linkit(){
   connection.query('update myverses set mp3 = ? where mp3 = "empty" limit 1', [link],
    function (error, results, fields) {
      console.log(error)

    })

}


function writeit() {
  const Polly = new AWS.Polly({
    signatureVersion: 'v4',
    region: 'us-east-1'
})

let params = {
    'Text': myverse.replace(" Job ", " Jobe "),
    'TextType': 'ssml',
    'OutputFormat': 'mp3',
    'VoiceId': 'Matthew'
}

Polly.synthesizeSpeech(params, (err, data) => {
    if (err) {
        console.log(err.code)
    } else if (data) {
        if (data.AudioStream instanceof Buffer) {
            Fs.writeFile(localfile, data.AudioStream, function(err) {
                if (err) {
                    return console.log(err)
                }
                console.log("Verse recorded successfully!")
            })
        }
    }
})

}

function uploadit () {
  console.log('Preparing to upload the verse.')
  s3 = new AWS.S3({apiVersion: '2006-03-01'});
 var uploadParams = {Bucket: 'myverses', key: '/test.mp3', Body: myvalue, ACL: 'public-read'};
  var file = localfile;

  var fs = require('fs');
  var fileStream = fs.createReadStream(file);
  fileStream.on('error', function(err) {
    console.log('File Error', err);
  });
  uploadParams.Body = fileStream;

  var path = require('path');
  uploadParams.Key = book.replace(/ /g, "")+reference.replace(/ /g, "")+".mp3";

  // call S3 to retrieve upload file to specified bucket
  s3.upload (uploadParams, function (err, data) {
    if (err) {
      console.log("Error", err);
    } if (data) {
      console.log("Upload Success", data.Location);
      linkit();
      addanother();
    }
  });

}

  });
}

1 个答案:

答案 0 :(得分:1)

尝试:

const path = require('path');
AWS.config.loadFromPath(path.join(__dirname, 'config.json'));

然后,如果这还不够,请尝试将'myverse.mp3'的所有引用更改为:

path.join(__dirname, 'myverse.mp3')

有关__dirname

的文档