【发布时间】:2016-02-14 10:44:07
【问题描述】:
我尝试从 Angular 连接到 Express.io 套接字,但出现错误 404 CORS。我该如何解决这个问题?
XMLHttpRequest cannot load http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1447367208172-29. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
如果我直接从浏览器直接加载这条路由,效果很好但是从 localhost:80 angular 到 localhot:3000 express.io 不起作用。
在我的 express.io 中,我禁用了 CORS,它适用于正常的 ajax 请求,但不适用于 socket.io:
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
next();
});
我在express.io中初始化socket.io:
app.http().io()
【问题讨论】:
标签: node.js express socket.io express.io