res.send()
expects a string argument. So, you have to pass a string.
If you want the browser to execute some Javascript, then what you send depends upon what kind of request is coming in from the browser.
If it's a browser page load request, then the browser expects an HTML response and you need to send an HTML page string back. If you want to execute Javascript as part of that HTML page, then you can embed a <script>
tag inside the page and then include Javascript text inside that <script>
tag and the browser will execute that Javascript when the page is parsed and scripts are run.
If the route is in response to a script tag request, then you can return Javascript text as a string and you need to make sure the MIME type appropriately indicates that it is a script.
If the route is in response to an Ajax call, then it all depends upon what the caller of the Ajax call expects. If they expect a script and are going to execute the text as Javascript, then you can also just send Javascript text as a string. If they expect HTML and are going to process it as HTML, then you probably need to embed the <script>
tag inside that HTML in order to get the Javascript executed.
In your example of:
response.send(blaBla());
That will work just fine if blaBla()
synchronously returns a string that is formatted properly per the above comments about what the caller is expecting. If you want further help with that, then you need to show or describe for us how the request is initiated in the browser and show us the code for the blaBla()
function because the issue is probably in the blaBla()
function.
There are lots of issues with things you have in your question:
You show req.send(alert('Hello world'))
in the text of your question. The .send()
method belongs to the res
object, not the req
object (the second argument, not the first). So, that would be res.send()
, not req.send()
.
In that same piece of code, there is no alert()
function in node.js, but you are trying to execute it immediately and send the result with .send()
. That won't work for a bunch of reasons.
Your first code block using blaBla()
will work just fine as long as blaBla()
returns a string of the right format that matches what the caller expects. If that doesn't work, then there's a problem with what blaBla()
is doing so we need to see that code.
Your second code block works because you are send a string which is something the caller is equipped to handle.
Update now that you've shown the code for blaBla()
.
Your code for blaBla()
does not return anything and it's asynchronous so it can't return the result. Thus, you cannot use the structure response.send(blaBla());
. There is no way to make that work.
Instead, you will need to do something different like:
blaBla(response);
And, then modify blaBla()
to call response.send(someTextValue)
when the response string is known.
function blaBla(res) {
var youtubedl = require('youtube-dl');
var url = 'http://www.youtube.com/watch?v=WKsjaOqDXgg';
// Optional arguments passed to youtube-dl.
var options = ['--username=user', '--password=hunter2'];
youtubedl.getInfo(url, options, function(err, info) {
if (err) {
res.status(500).send("Internal Error");
} else {
console.log('id:', info.id);
console.log('title:', info.title);
console.log('url:', info.url);
console.log('thumbnail:', info.thumbnail);
console.log('description:', info.description);
console.log('filename:', info._filename);
console.log('format id:', info.format_id);
// construct your response here as a string
res.json(info);
}
});
}
Note also that the error handling does not use throw
because that is really not useful inside an async callback.