使用Jade/Pug在网页上输出音频时出现404错误
404 error when using Jade/Pug to output audio on a webpage
我正在尝试通过网页使用 IBM Watson Text to Speech api。在网页上,我有一个表格,其中包含用户想要阅读的句子或单词,以及一个按钮 "SPEAK!",用于完成和阅读表格输入。我的问题是,当我查看控制台以确保一切顺利时,我发现找不到我的音频文件。
App.js -- 这是连接到 api 的代码,然后将合成代码发送到可以访问它们的 "audioFiles/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')
特定错误 - GET 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
错误。
我正在尝试通过网页使用 IBM Watson Text to Speech api。在网页上,我有一个表格,其中包含用户想要阅读的句子或单词,以及一个按钮 "SPEAK!",用于完成和阅读表格输入。我的问题是,当我查看控制台以确保一切顺利时,我发现找不到我的音频文件。
App.js -- 这是连接到 api 的代码,然后将合成代码发送到可以访问它们的 "audioFiles/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')
特定错误 - GET 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
错误。