Javascript Angular2 Heroes示例与node.js服务器结合使用时会加倍发布
我已经在第10步中设置了英雄示例。Http客户端。我已经将示例运行到,运行良好。现在,我用端口3002上的Node.JS服务器替换“虚拟”服务器,将_HeroesURL替换为“http:/localhost:3002/hero”。从那以后,GET运行正常,POST(附加到addHero)被调用两次并出错 My Node.js服务器:Javascript Angular2 Heroes示例与node.js服务器结合使用时会加倍发布,javascript,node.js,angular2-http,Javascript,Node.js,Angular2 Http,我已经在第10步中设置了英雄示例。Http客户端。我已经将示例运行到,运行良好。现在,我用端口3002上的Node.JS服务器替换“虚拟”服务器,将_HeroesURL替换为“http:/localhost:3002/hero”。从那以后,GET运行正常,POST(附加到addHero)被调用两次并出错 My Node.js服务器: var dbHeroes = [ {"id": 11, "name": "Mr. Nice"}, {"id": 12, "name": "Narco"}, {"id
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)
var-dbherones=[
{“id”:11,“姓名”:“尼斯先生”},
{“id”:12,“name”:“Narco”},
{“id”:13,“name”:“Bombasto”},
{“id”:14,“name”:“Celeritas”},
{“id”:15,“name”:“Magneta”},
{“id”:16,“name”:“ruberman”},
{“id”:17,“name”:“Dynama”},
{“id”:18,“name”:“Dr IQ”},
{“id”:19,“name”:“Magma”},
{“id”:20,“名称”:“龙卷风”}]
var-id=21;
var http=require('http');
http.createServer(函数(req,res){
//批准XHR请求
res.setHeader(“访问控制允许原点”,“*”);
res.setHeader(“访问控制允许头”,“内容类型”);
res.setHeader(“访问控制请求方法”、“获取POST”);
//设置一些路线
开关(请求url){
案例“/英雄”:
开关(请求方法){
案例“GET”:
//获得英雄
res.writeHead(200,“OK”,{'Content-Type':'application/json'});
res.write(JSON.stringify(dbheros));
res.end();
打破
案例“选项”:
//飞行前请求(XHR)
res.writeHead(200,“OK”,{'Content-Type':'text/plain'});
res.end('Allow:GET POST');
打破
案例“职位”:
变量体=“”;
//得到一大块尸体
请求打开(“数据”,功能(块){
body+=块;
})
//车身完成
请求开启(“结束”,函数(){
var newHero=JSON.parse(body);
控制台日志(“接收英雄:+身体”);
newHero.id=id++;
log(“英雄现在是:+JSON.stringify(newHero));
dbheros.push(newHero);
res.writeHead(200,“OK”,{'Content-Type':'application/json'});
res.write(JSON.stringify(newHero));
res.end();
})
打破
违约:
控制台日志(“[501]请求:未知方法”+请求方法);
res.writeHead(501,“未实现”,{'Content-Type':'text/html'});
res.end('501-未实现未实现!');
打破
}
打破
违约:
//在所有其他路线上找不到返回路线
res.writeHead(404,“未找到”,{'Content-Type':'text/html'});
res.end(“未找到404
找不到请求的url找不到。
");
}听(3002);//侦听tcp端口3002(所有接口)
找到了答案:BrowserSync发现两个Angular2应用程序实例正在运行,并在两个实例上同步单击按钮。通过关闭此“功能”修复…客户端代码在哪里?它与angular.io上的Heroes示例完全相同:问题似乎是特定于浏览器的。在Firefox上,POST运行两次。今天早上,我尝试了Chrome,它运行了一次帖子,然后遇到了一个错误:`angular2.dev.js:23524 EXCEPTION:TypeError:无法读取[{hero.name}}中未定义的属性'name'HeroListComponent@2:31]angular2.dev.js:23514异常:TypeError:无法读取[{{hero.name}]中未定义的属性“name”在里面HeroListComponent@2:31]BrowserDomAdapter.logError@angular2.dev.js:23514BrowserDomAdapter.logGroup@angular2.dev.js:23525ExceptionHandler.call@angular2.dev.js:1145(匿名函数