TypeError:res.setHeader不是函数,在启用cors时出现此错误



I found many similar problems but couldn't resolve this

我的主要目标是从客户端发出API请求,通过该请求我可以向服务器发送JSON文件,我需要将其存储在数据库中,但由于我的前端和后端托管在不同的域上,我需要启用交叉原始资源共享(CORS(
要启用CORS,我使用了此处指定的方法用选项配置CORS(https://stackabuse.com/handling-cors-with-node-js/)

这就是我从react前端发送post请求的方式,此处apiUrl=http://localhost:${port}/apireact

fetch(apiUrl, {
method: 'POST',
body: JSON.stringify({name: "thunder"}),
})
.then(data => console.log(data))
.catch(err => console.error(err));

代码截图

我的服务器端代码是这个,

import Koa from "koa";
import next from "next";
import Router from "koa-router";
const cors = require('cors')
const originUrl=process.env.HOST;  //HOST=https://<somenumber>.ngrok.io
const app = next({
dev,
});
app.prepare().then(async () => {
const server = new Koa();
const router = new Router();
/********CORS******************/
const corsOptions = {
origin: originUrl,
optionsSuccessStatus: 200, // For legacy browser support
methods: ['GET','POST']
}
server.use(cors(corsOptions));
router.post("/apireact",(req,res)=>{
res.json({
message: 'Hello World2'
});
});
/**************************/
server.use(router.allowedMethods());
server.use(router.routes());
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
});

服务器代码截图

在运行的服务器上,我收到以下错误,

TypeError: res.setHeader is not a function
┃       at applyHeaders (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:153:15)
┃       at applyHeaders (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:149:11)
┃       at applyHeaders (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:149:11)
┃       at cors (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:187:7)
┃       at C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:224:17
┃       at originCallback (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:214:15)
┃       at C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:219:13
┃       at optionsCallback (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:199:9)
┃       at corsMiddleware (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulescorslibindex.js:204:7)
┃       at dispatch (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_moduleskoanode_moduleskoa-composeindex.js:42:32)
┃       at C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modules@shopifykoa-shopify-authdistsrcauthindex.js:111:51
┃       at step (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulestslibtslib.js:133:27)
┃       at Object.next (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulestslibtslib.js:114:57)
┃       at C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulestslibtslib.js:107:75
┃       at new Promise (<anonymous>)
┃       at Object.__awaiter (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modulestslibtslib.js:103:16)
┃       at shopifyAuth (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_modules@shopifykoa-shopify-authdistsrcauthindex.js:40:24)
┃       at dispatch (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_moduleskoanode_moduleskoa-composeindex.js:42:32)
┃       at C:UsersKulbhushan goswamilearning_sitemy_email_appnode_moduleskoanode_moduleskoa-composeindex.js:34:12
┃       at Application.handleRequest (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_moduleskoalibapplication.js:168:12)
┃       at Server.handleRequest (C:UsersKulbhushan goswamilearning_sitemy_email_appnode_moduleskoalibapplication.js:150:19)
┃       at Server.emit (events.js:315:20)

请建议我该怎么修?

来自文档https://github.com/koajs/router/blob/master/API.md,Koa路线将看起来像

router.get('/', (ctx, next) => {
});

在您的代码中,您将其用作

router.post("/apireact",(req,res)=>{
res.json({
message: 'Hello World2'
});
});

我猜这就是问题所在。

cors是一个不能直接与koa一起使用的express中间件。

您可以使用以下类似koa-connect的内容,这是不推荐的。

import k2c from 'koa-connect';
app.use(k2c(cors()));

建议使用@koa/cors

最新更新