Angular2 英雄示例在与节点.js服务器结合使用时使 POST 翻倍
Angular2 Heroes example doubles POST when combined with node.js server
我已经设置了英雄示例到步骤 10。Http 客户端。我已经运行了示例,运行良好。现在,我将端口 3002 上的 Node.JS 服务器替换"虚拟"服务器,将_HeroesURL替换为"http:/localhost:3002/hero"。从那以后的 GET 运行良好,POST(附加到 addHero)被调用两次出错?
我的节点.js服务器:
var dbHeroes = [
{"id": 11, "name": "Mr. Nice"},
{"id": 12, "name": "Narco"},
{"id": 13, "name": "Bombasto"},
{"id": 14, "name": "Celeritas"},
{"id": 15, "name": "Magneta"},
{"id": 16, "name": "RubberMan"},
{"id": 17, "name": "Dynama"},
{"id": 18, "name": "Dr IQ"},
{"id": 19, "name": "Magma"},
{"id": 20, "name": "Tornado"}]
var id = 21;
var http = require('http');
http.createServer(function (req, res) {
// approve XHR-requests
res.setHeader("Access-Control-Allow-Origin","*");
res.setHeader("Access-Control-Allow-Headers", "Content-type");
res.setHeader("Access-Control-Request-Method","GET POST");
// set up some routes
switch (req.url) {
case "/hero":
switch (req.method) {
case "GET":
// getting heroes
res.writeHead(200, "OK", {'Content-Type': 'application/json'});
res.write(JSON.stringify(dbHeroes));
res.end();
break;
case "OPTIONS":
// pre-flight request (XHR)
res.writeHead(200, "OK", {'Content-Type': 'text/plain'});
res.end('Allow: GET POST');
break;
case "POST":
var body = '';
// get a chunk of the body
req.on("data", function(chunk) {
body += chunk;
})
// body finished
req.on("end", function () {
var newHero = JSON.parse(body);
console.log("recieved hero: " + body);
newHero.id = id++;
console.log("hero is now:" + JSON.stringify(newHero));
dbHeroes.push(newHero);
res.writeHead(200, "OK", {'Content-Type': 'application/json'});
res.write(JSON.stringify(newHero));
res.end();
})
break;
default:
console.log(" [501] Request: unknown method " + req.method);
res.writeHead(501, "Not implemented", {'Content-Type': 'text/html'});
res.end('<html><head><title>501 - Not implemented</title></head><body><h1>Not implemented!</h1></body></html>');
break;
}
break;
default:
// return not found on all other routes
res.writeHead(404, "Not found", {'Content-Type': 'text/html'});
res.end("<html><head>404 Not Found</head>
<body><h1>Not Found</h1>The requested url was not found.</body>
</html>");
};}).listen(3002); // listen on tcp port 3002 (all interfaces)
找到答案:BrowserSync 发现 Angular2-app 的两个实例正在运行,并在两个实例上同步了按钮的点击。通过关闭此"功能"来修复....
相关文章:
- javascript结合了数组和字典
- 将Firebase与Electron结合使用
- 结合jQuery和jetpack无限滚动
- 将Angular js与taglib结合使用
- 将图像上传ajax与表单提交ajax相结合
- 将语句与jquery相结合,并使用媒体查询来实现返回页首按钮
- 将broccoliJS与当前使用requireJS的模块化主干应用程序结合使用,我想完全放弃requireJS
- 图像Blob>上传后是其大小的50倍
- jQuery结合了2个绑定函数(类型错误:a是未定义的)
- 媒体查询与Javascript相结合
- JQuery 事件每次翻倍
- 每当用户滚动到Chart.js时,它的大小就会翻倍
- 为什么每次通话都翻倍
- Angular2 英雄示例在与节点.js服务器结合使用时使 POST 翻倍
- 时间计数翻倍
- 输出数量翻倍
- 每当我保存文档编辑器添加额外的空格、段落和换行符时,换行符和段落就会翻倍
- 翻倍.parseDouble不工作在我的网页上
- Web Workers——如果对象是按值传递的,内存使用量会翻倍吗?
- 在MongoDB文档中强制Javascript数字翻倍