running function after res.send

Eli

I'm trying to run this code

module.exports = async (req, res, next) => {

    res.set('Content-Type', 'text/javascript');

    const response = {};

    res.status(200).render('/default.js', { response });

    await fn(response);
};

fn is a function that calls an api to a service that will output to the client something. but its dependent on the default.js file to be loaded first. How can do something like

res.render('/default.js', { response }).then(async() => {
    await fn(response);
};

tried it, but doesn't seem to like the then()

also, fn doesn't return data to the client, it calls an api service that is connected with the web sockets opened by the code from default.js that is rendered.

do i have to make an ajax request for the fn call and not call it internally?

any ideas?

jfriend00

Once you call res.render(), you can send no more data to the client, the http response has been sent and the http connection is done - you can't send any more to it. So, it does you no good to try to add something more to the response after you call res.render().

It sounds like you're trying to put some data INTO the script that you send to the browser. Your choices for that are to either:

  1. Get the data you need to with let data = await fn() before you call res.render() and then pass that to res.render() so your template engine can put that data into the script file that you send the server (before you send it). You will need to change the script file template to be able to do this so it has appropriate directives to insert data into the script file and you will have to be very careful to format the data as Javascript data structures.

  2. Have a script in the page make an ajax call to get the desired data and then do your task in client-side Javascript after the page is already up and running.


It looks like it might be helpful for you to understand the exact sequence of things between browser and server.

  1. Browser is displaying some web page.
  2. User clicks on a link to a new web page.
  3. Browser requests new web page from the server for a particular URL.
  4. Server delivers HTML page for that URL.
  5. Browser parses that HTML page and discovers some other resources required to render the page (script files, CSS files, images, fonts, etc...)
  6. Browser requests each of those other resources from the server
  7. Server gets a request for each separate resource and returns each one of them to the browser.
  8. Browser incorporates those resources into the HTML page it previously downloaded and parsed.
  9. Any client side scripts it retrieved for that page are then run.

So, the code you show appears to be a route for one of script files (in step 5 above). This is where it fits into the overall scheme of loading a page. Once you've returned the script file to the client with res.render(), it has been sent and that request is done. The browser isn't connected to your server anymore for that resource so you can't send anything else on that same request.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

after using parenthesis () my function not running?

分類Dev

Interrupting an unknown long-running function after a certain time period

分類Dev

Send MySql result in res.send

分類Dev

What is the difference between res.end() and res.send()?

分類Dev

difference between res.send or res.json

分類Dev

Angular - res.json() is not a function

分類Dev

Node.js res.send VS res.end VS return res.end

分類Dev

response.send is not a function

分類Dev

Send function Rails jBuilder

分類Dev

Javscript function running slow

分類Dev

Check URL and running function?

分類Dev

Getting error after email send

分類Dev

How to send a value to exec's running code

分類Dev

Send data and user commands to running Puppeteer script

分類Dev

res.send、後に終了する方法は?

分類Dev

running JavaFX application after jpackage

分類Dev

python code after finally is not running

分類Dev

Express.jsのres.sendとres.jsonの違い

分類Dev

res.sendおよびres.renderの呼び出し

分類Dev

res.sendまたはres.jsonの違い

分類Dev

Cancel currently running function/goroutine

分類Dev

Running program/function in background in Python

分類Dev

Run a function after each function

分類Dev

Excel animations running very slow after running Macro

分類Dev

Keep Outlookinstance after Messageitem.send

分類Dev

send property value to server after stabilization

分類Dev

Send several waiting messages after a waiting message

分類Dev

Underscore after function?

分類Dev

Unmock function after mockimplementation