这是我第一次尝试使用 Socket.io 我正在创建一个 api 来发出一些数据。它工作正常。这是我认为需要修改的代码:
app.use(express.static(__dirname + '/node_modules'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
res.setHeader('Access-Control-Allow-Origin', '*');
});
参考上面的代码,数据正在成功发送到 index.html 文件。我使用的端口是 4200。所以如果我在浏览器中输入 localhost:4200,我就能看到 index.html 文件中显示的数据。现在我想在我的 angular js 项目中使用 localhost:4200 来获取数据,但我获取的是 index.html 文件的 html 内容。
这是我的 angularjs 代码片段:
setInterval(function(){
$http.get("localhost:4200").success(function(response) {
console.log(response);
})
}, 500);
我在这里做错了什么?
我能够通过添加以下角度片段来使其工作:
$scope.server = "100.20.32.20"; //Ip of my machine
$scope.socket = io('http://' + server + ':4200');
socket.on('broad', function(data) {
console.log($scope.data);});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句