In html, i am passing the value to the node js file in post form using the form
tag.
I created a new value on the server side with the value passed in html.
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.
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')
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.
I have succeeded in compiling the value written in html textarea
on the server side and extracting the result.
But I do not know how to send the value back to html.
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();
});
Hope it will be helpfull
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.