node.js - 请求头 {Content-Type : application/json, Content-Length: 188 ,} 抛出错误 SyntaxError: Unexpected end of JSON input
问题描述
在请求中,我使用标头 {Content-Type : application/json, Content-Length: 188 ,} 服务器会引发错误。
SyntaxError: JSON.parse () 解析时 JSON 输入意外结束 (C:\Users\darkc\Desktop\\server\node_modules\body-parser\lib\types\json.js:89:19)
这是我的代码
索引.ts
import { ApolloServer } from "apollo-server-express";
import cookieParser from "cookie-parser";
import cors from "cors";
import { config } from "dotenv";
import express from "express";
import { express as voyagerMiddleware } from "graphql-voyager/middleware";
import models from "./models";
import resolvers from "./resolvers";
import typeDefs from "./schema";
import authorization from "./toolses/authorization";
import adminInitializeApp from "./toolses/function/adminInitializeApp";
import {} from "./toolses/function/globalType";
config();
(async () => {
const app = express();
const { PORT, FRONT_PORT, APOLLO_PATH } = process.env;
const corsOptions = {
origin: FRONT_PORT,
credentials: true,
};
app.use("/voyager", voyagerMiddleware({ endpointUrl: "/graphql" }));
app.use(cors(corsOptions));
app.use(cookieParser());
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ req, res }) => {
const currentUser = authorization(req, res);
const db = adminInitializeApp();
return { res, req, db, currentUser, models };
},
});
server.applyMiddleware({ app, path: APOLLO_PATH, cors: false });
app.listen({ port: PORT }, () => {
console.log(Apollo Server on http://localhost:${PORT}/graphql);
});
})();
标题
Content-Type: application/json
Content-Length: 188
解决方案
推荐阅读
- android - 膨胀视图后膨胀视图方法不起作用
- javascript - 如何在 react js 应用程序中使用 react-gtm-module?
- apache-flink - Flink SQL 从无到有
- flutter - Firebase 消息传递没有默认构造函数
- sympy - 响应 simple(...) 的 Sympy 容器对象
- python - 在pandas管道中的pivot()之后折叠多索引
- javascript - 如何在 aws lambda、nodejs 14.x 上托管一个 react、typescript 网站
- python - 在 Pandas 中替换?Python
- java - 无法计算字符串的每个部分中有多少个唯一日期可用
- python - 从其他列计算 Panda 数据框中的 np.arange