If you want to have a preprocessing step, then you need an API to take the commands, process them, run them, and then return the output. If you want to stream the output, you should look into something like websockets. There are also libraries built specifically for streaming logs to the browser, e.g. [0]
But might I suggest a simpler approach? How about a JS terminal emulator that is SSHed into the raspberry pi? I've used a library called "wetty" [1] successfully in the past. It's a node server + JS terminal emulator (based on hterm, used in chromebooks). The node server SSH's into the target box on behalf of the client, and then forwards the tty over the link `client <-> server <-> ssh <-> target`. All you need to do is install the node server on the pi and have it SSH into localhost.