简体   繁体   中英

Can I use certain client-side Javascript APIs server-side?

I'm looking at some APIs which are meant to be used in a web browser. They require the inclusion of their .js code and then present you with an API that returns audio. Restating this in a more general way, these APIs return some stream of bytes (audio in this case) which the browser plays.

Can APIs of this type be used in server-side Javascript frameworks, eg. Node, where I make the API call and capture the resulting binary data in some variable? I'm not sure I know how to map the semantics of what's going on in the browser to a server-side solution.

Here's an arbitrary example http://responsivevoice.org/api/

You include their Javascript in your page and then call sampleLibrary.speak("hello world"); . This results in audio coming back to the browser which is played.

Generally many JavaScript's created for the browser can be run, but you have to mock a lot of browser functionality.

I took a look at the library you were linking to. Beautified it, and made some changes to it to make it a node module. Then you have to mock navigator.userAgent , window and document .

For document the library is using addEventListener . After mocking those I'm getting RV: Voice synthesis not supported . The library is using Web Speech API through Window.speechSynthesis . This is not possible to mock.

It does however have a fallback functionality, which is to insert AUDIO tags through createElement . You will then get urls for generated audio files. Then you might be able to download these and do what you wanted to do on your server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM