[英]How to pass data from a node js file to an html file
In html, i am passing the value to the node js file in post form using the form
tag. 在html中,我正在使用
form
标签将值传递给post形式的node js文件。
I created a new value on the server side with the value passed in html. 我在服务器端使用html中传递的值创建了一个新值。
I want to insert the value back into the textbox
(result) on the html. 我想将值重新插入html的
textbox
(结果)中。 And I want this process to happen on the same page. 我希望此过程在同一页面上进行。
I am writing html with the pug template engine and router name is form. 我正在用哈巴狗模板引擎编写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> 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')
The html file is created in this way and is sending these values to the server using the post
method. 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);
});
}
});
});
It is an excerpt from the post part of the code. 它摘自代码的后半部分。
I have succeeded in compiling the value written in html textarea
on the server side and extracting the result. 我已经成功地在服务器端编译了以html
textarea
编写的值,并提取了结果。
But I do not know how to send the value back to html. 但是我不知道如何将值发送回html。
You can use express js middleware to pass a data from node.js server tk all views Like this 您可以使用express js中间件从node.js服务器tk的所有视图传递数据,就像这样
app.use(function(req,res,next){
res.locals.yourvariable = req.yourvariable;
next();
});
Hope it will be helpfull 希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.