Does the ctx.render () load page need to wait for all subsequent execution to be completed before the final result is displayed?

the business you want to achieve is to render the page first, and then add content to the page through ctx.body

the core code is as follows

await ctx.render("crawler", {
    title: "",
    content: `<h2></h2>
                <h4></h4>`
})
const linkPool = createLinkPool()
for (let i = 0; i < linkPool.length; PPi) {
    const html = await requestPage(linkPool[i])
    let builds = await dataHandler(html)
    let arr = [];

    const sql = "insert into buildinfo(id,name,area,address,average,price,description,type) values ?"
    //let rows = await query(sql, [builds])
    try {
        let rows = await query(sql, [builds])
        ctx.body += `${linkPool[i]}${rows.affectedRows}`

    } catch (error) {
        ctx.body += `${linkPool[i]}:${error}`
    }
}

the problem now is that the page will be displayed only when all the operations to write to the database are over, otherwise I have been reading the small circle all the time. How can I achieve the desired effect

< hr >

I put the operation after rendering the page into a middleware with the following code

router.get("/crawler/", async (ctx, next) => {
    await ctx.render("crawler", {
        title: "",
        content: `<h2></h2>
                    <h4></h4>`
    })
    next()
})
The

page does perform the logic of storage operation after rendering, but

ctx.body += `${linkPool[i]}${rows.affectedRows}`

this operation does not write content to the page. Is my usage wrong? what does ctx.body do?


you are await query . Of course, you will return after reading it.
if you need to render and then populate the data, you can only write a script at the front end to access the back-end interface, because http cannot actively write data.


the best way to meet this requirement of yours is to separate the front and rear ends

.

there is a static page at the front end, and then request the interface data in the background to fill the page

The

ctx.body method returns data to the front end, which is equivalent to the content of response.body

.

what you need should be implemented in the bigpipe way. The segmented return content. Search for how to combine your implementation with the specific implementation

.
Menu