这不是我的代码。我的问题是,如何在html标签中显示'+ result +'之类的内容,以及在post响应中输入文本框的值中,它是如何有效的?... 另外,在旁注中,为什么当我执行response.write(form),然后是response.end()作为response.end(form)的替代,代码停止工作。我是节点的新手,所以请把它弄清楚。
// Load the http module to create an http server.
var http = require('http');
// Create a function to handle every HTTP request
function handler(req, res){
var form = '';
if(req.method == "GET"){
form = '<!doctype html> \
<html lang="en"> \
<head> \
<meta charset="UTF-8"> \
<title>Form Calculator Add Example</title> \
</head> \
<body> \
<form name="myForm" action="/" method="post">\
<input type="text" name="A"> + \
<input type="text" name="B"> = \
<span id="result"></span> \
<br> \
<input type="submit" value="Submit"> \
</form> \
</body> \
</html>';
//respond
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(form);
} else if(req.method == 'POST'){
//read form data
req.on('data', function(chunk) {
//grab form data as string
var formdata = chunk.toString();
console.log(formdata);
//grab A and B values
console.log(formdata.split("&")[0]);
var a = eval(formdata.split("&")[0]);
console.log(a);
var b = eval(formdata.split("&")[1]);
var result = calc(a,b);
//console.log(chunk.toString());
//console.log(a);
//console.log(b);
//console.log(result);
//fill in the result and form values
form = '<!doctype html> \
<html lang="en"> \
<head> \
<meta charset="UTF-8"> \
<title>Form Calculator Add Example</title> \
</head> \
<body> \
<form name="myForm" action="/" method="post">\
<input type="text" name="A" value="'+a+'"> + \
<input type="text" name="B" value="'+b+'"> = \
<span id="result">'+result+'</span> \
<br> \
<input type="submit" value="Submit"> \
</form> \
</body> \
</html>';
//respond
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(form);
});
} else {
res.writeHead(200);
res.end();
};
};
//js functions running only in Node.JS
function calc(a,b){
return Number(a)+Number(b);;
}
http.createServer(handler).listen(8080, function(err){
if(err){
console.log('Error starting http server');
} else {
console.log("Server running at http://127.0.0.1:8080/");
};
});