404使用Jade/Pug在网页上输出音频时的错误



我试图通过网页使用IBM Watson文本将语言API进行语音API。在网页上,我有一个表格,该表格获取用户想要读取的句子或单词,并且一个按钮"说话!"最终确定并读取表单输入。我的问题是,当我在控制台上确保一切顺利时,我会发现我的音频文件没有找到。

app.js-这是连接到API的代码,然后将合成代码发送到可以访问的" Audiiofiles/output.ogg"的代码。

var input;
app.get("/speech", function(req, res, next) {
 input = req.query.speech;
 const TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
 const fs = require('file-system');
 if(input) {
   var intentValNumber = input.length;
 }
 console.log(intentValNumber);
 if (intentValNumber > 0) {
   const text_to_speech = new TextToSpeechV1({
       url: "URL is here",
       username: 'UserName is here',
       password: 'PassWord is here',
       version_date: TextToSpeechV1.VERSION_DATE_2017_04_26
     });
     var params = {
       text: input,
       voice: 'en-US_MichaelVoice',
       accept: 'audio/ogg'
     };
     console.log(input);
  // Pipe the synthesized text to a file.
   text_to_speech.synthesize(params).on('error', function(error) {
     console.log('Error:', error);
   }).pipe(fs.createWriteStream('audioFiles/output.ogg'));
 };
next();
});

speech.pug-这是应执行表单和音频的地方。

extends layout
block content
 .main.container.row
  .col-md-6.col-md-offset-3
    h1.display-4.m-b-2 Magic Reading!
     form(method="GET" action="/speech")
      div.form-group
       label(for='speech') Type something to be spoken:
       input#speech.form-control(type='text', placeholder='Type something to be read' name='speech')
       button.btn.btn-primary(type='submit') SPEAK!
       audio(controls='', autoplay='')
         source(src='../audioFiles/output.ogg', type='audio/ogg')

特定错误 - 获取http://localhost:3000/audiofiles/output.ogg

事先感谢您的任何建议!还让我知道是否有人需要有关错误或代码的更多信息。

编辑 - 这是我目录结构的简化版本...

 Project
  |
  +-- audioFiles(folder)
  |   |
  |   + - output.ogg
  |   + - music.mp3
  |
  +-- public(folder)
  |     |
  |     + - images(folder)
  |     + - stylesheets(folder)
  |    
  +-- routes(folder)
  |    |  
  |    +-- index.js
  |    
  +-- views(folder)
  |    |  
  |    +-- speech.pug
  |    
  + -- app.js

在您的 app.js

中添加以下行

app.use("/audioFiles", express.static(path.join(__dirname, "audioFiles")))

然后,http://localhost:3000/audiofiles/output.ogg不应给出404错误。

最新更新