如何将数据从节点js文件传递到html文件 [英] How to pass data from a node js file to an html file
问题描述
在html中,我正在使用form
标签将值以发布形式传递给node js文件.
In html, i am passing the value to the node js file in post form using the form
tag.
我在服务器端使用html中传递的值创建了一个新值.
I created a new value on the server side with the value passed in html.
我想将值重新插入到html的textbox
(结果)中.
我希望这个过程在同一页面上进行.
I want to insert the value back into the textbox
(result) on the html.
And I want this process to happen on the same page.
我正在用哈巴狗模板引擎编写html,路由器名称为form.
I am writing html with the pug template engine and router name is 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> int main() { return 0; }
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
方法将这些值发送到服务器.
The html file is created in this way and is sending these values to the server using the post
method.
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);
});
}
});
});
这是代码后部分的摘录.
It is an excerpt from the post part of the code.
我已经成功地在服务器端编译了用html textarea
编写的值,并提取了结果.
I have succeeded in compiling the value written in html textarea
on the server side and extracting the result.
但是我不知道如何将值发送回html.
But I do not know how to send the value back to html.
推荐答案
您可以使用express js中间件从node.js服务器tk的所有视图传递数据 像这样
You can use express js middleware to pass a data from node.js server tk all views Like this
app.use(function(req,res,next){
res.locals.yourvariable = req.yourvariable;
next();
});
希望这会有所帮助
这篇关于如何将数据从节点js文件传递到html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!