繁体   English   中英

如何将数据从节点js文件传递到html文件

[英]How to pass data from a node js file to an html file

在html中,我正在使用form标签将值传递给post形式的node js文件。

我在服务器端使用html中传递的值创建了一个新值。

我想将值重新插入html的textbox (结果)中。 我希望此过程在同一页面上进行。

我正在用哈巴狗模板引擎编写html,路由器名称是form。

form(action='form' method='post')
            p.lead
                textarea(style='resize:none; width:400px; height:300px;' name='description' onkeydown="if(event.keyCode===9){var v=this.value,s=this.selectionStart,e=this.selectionEnd;this.value=v.substring(0, s)+'\t'+v.substring(e);this.selectionStart=this.selectionEnd=s+1;return false;}") #include<stdio.h>&#10;int main()&#10;{&#10;&#10;    return 0;&#10;}
            p
                input(type='text' style='resize:none; width:400px; height:50px;' name='result' readonly='readonly')
            p
                input(type='submit' value='submit' class='btn btn-success')

html文件是以这种方式创建的,并使用post方法将这些值发送到服务器。

app.post('/form',function(req,res) {
var description = req.body.description;
var source = description.split(/\r\n|\r\n/).join("\n");

fs.writeFile(file,source,'utf8',function(error) {
    console.log('write end');
});
var compile = spawn('gcc',[file]);
compile.stdout.on('data',function(data) {
    console.log('stdout: '+data);
});
compile.stderr.on('data',function(data){
    console.log(String(data));
});
compile.on('close',function(data){
    if(data ==0) {
        var run = spawn('./a.out',[]);
        run.stdout.on('data',function(output){
            console.log('end');
        });
        run.stderr.on('data', function (output) {
        console.log(String(output));
        });
        run.on('close', function (output) {
        console.log('stdout: ' + output);
        });
        models.Code.create( {
            title: 'test1',
            code: source 
        })
        .catch(err => {
            console.error(err);
        });
    }
  });
});

它摘自代码的后半部分。

我已经成功地在服务器端编译了以html textarea编写的值,并提取了结果。

但是我不知道如何将值发送回html。

您可以使用express js中间件从node.js服务器tk的所有视图传递数据,就像这样

app.use(function(req,res,next){
res.locals.yourvariable = req.yourvariable;
next();
});

希望对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM