求教——使用node做表单,刷新浏览器页面,浏览器为什么会重复提交上次所填的信息

时间:2022-03-15 05:21:29

最近在学些node,按照《nodejs实战》上的代码做练习,发现有表单重复提交的问题

第一次打开页面,显示如图是get请求

  求教——使用node做表单,刷新浏览器页面,浏览器为什么会重复提交上次所填的信息

       图1

现在我们提交aaa,显示如图,post请求

  求教——使用node做表单,刷新浏览器页面,浏览器为什么会重复提交上次所填的信息

      图2

刷新页面,应该显示图1的,结果确实下面这张图,post请求。即使是按ctrl+f5刷新没用,浏览器还是重复提交了上次的内容,显示如图

  求教——使用node做表单,刷新浏览器页面,浏览器为什么会重复提交上次所填的信息

      图3

不断的刷就这样了,求教这个是为什么?

  求教——使用node做表单,刷新浏览器页面,浏览器为什么会重复提交上次所填的信息

      图4

node代码如下

var http=require("http");
var qs = require("querystring");
var items=[];
var server=http.createServer(function(req,res){
console.log("req.url",req.url);
console.log("req.method",req.method);
if("/"==req.url){
switch(req.method){
case "GET":
show(res);
break;
case "POST":
add(req,res);
break;
default:
badRequest(res);
}
}else{
notFound(res);
}
})
server.listen(3000);
function show(res){
var html='<html><head><title>Todo List</title></head><body>'
+'<h1>Todo List</h1>'
+'<ul>'
+items.map(function(item){
return '<li>'+item+'</li>'
}).join('')
+'</ul>'
+'<form method="post" action="/">'
+'<p><input type="text" name="item"/></p>'
+'<p><input type="submit" value="Add Item"/></p>'
+'</form></body></html>';
res.setHeader("Content-Type","text/html");
res.setHeader("Content-Length",Buffer.byteLength(html));
res.end(html);
}
function notFound(res){
res.statusCode=404;
res.setHeader("Content-Type","text/plain");
res.end("notFound");
}
function badRequest(res){
res.statusCode=400;
res.setHeader("Content-Type","text/plain");
res.end("Bad Request");
}
function add(req,res){
var body='';
req.setEncoding("utf-8");
req.on("data",function(chunk){
console.log("chunk",chunk);
body+=chunk;
})
req.on("end",function(){
var obj=qs.parse(body);
items.push(obj.item);
show(res);
})
}