【发布时间】:2018-03-15 09:29:40
【问题描述】:
这是我的服务器代码:
import express from 'express';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
const app = express();
// app.use(cors()); // Also tried this
app.use(cors({ credentials: true, origin: true }));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/', (req, res) => res.json({ message: 'OK' }));
app.listen(3000);
这是我的网站代码(来自localhost:5000):
const response = await superagent.get('localhost:3000/')
.withCredentials()
.send();
但这被CORS阻止了:
错误:请求已终止 可能原因:网络离线、Access-Control-Allow-Origin不允许Origin、正在卸载页面等
为什么?
我使用的是 Firefox 58 和 Node.js 6.10。
【问题讨论】:
标签: node.js express superagent