gpt4 book ai didi

node.js - Node js/express/oboe.js 和 pug : Get node() events to update the dom

转载 作者:太空宇宙 更新时间:2023-11-04 00:35:41 25 4
gpt4 key购买 nike

第一次使用 Node 和模板引擎(是的,我知道,“开始”的地方很糟糕,但无论如何)。我正在从 json 流构建一些内容,最后我想将此内容吐出到我的 html 页面中的 div,如下(更新)expressjs 片段所示:

app.get('/foo', function (req, res) {

//... get my stream called 'mystream'

//... get ready to append content for items found in json stream
var htmlbuf = "";

//now process the json stream:
oboe(mystream)
.node('{bar}', function(aBar){
//eventually, I'd like to actually append individual new DOM elements,
//but for now I'll settle for just spitting out raw html:
var buffer = '<p>Found a bar with name'+ aBar.name + '</p>';
res.render('index', {itemsfound: htmlbuf});
});
});

索引.pug:

doctype html
html
head
link(type='text/css', rel='stylesheet', href='./css/main.css')
title Hello
body
h1 Hello world!
div#results.listing
items= itemsfound

我收到错误“错误:发送后无法设置 header ”。所以我认为问题是 oboe.node() 随时都会触发,而我没有在正确的时间发送响应内容?连接 oboe.node() 事件所需的代码/框架是什么,以便它们可以在我的 pug 模板中定位或创建 dom 元素并正确发送响应?谢谢。

最佳答案

你需要做这样的事情:

app.get('/foo', function (req, res, next) {

//... get my stream called 'mystream'

//... get ready to append content for items found in json stream
var htmlbuf = "";

//now process the json stream:
oboe(mystream)
.node('{bar}', function(aBar){
//eventually, I'd like to actually append individual new DOM elements,
//but for now I'll settle for just spitting out raw html:
htmlbuf += '<p>Found a bar with name' + aBar.name + '</p>';
})
.on('fail', function (err) {
res.statusCode = err.statusCode
next(err.thrown)
})
.on('done', function () {
res.render('index', {itemsfound: htmlbuf});
});
});

关于node.js - Node js/express/oboe.js 和 pug : Get node() events to update the dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39150564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com