首页 > 解决方案 > 请求头 {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

标签: node.jsgraphqlcorsapollo-server

解决方案


推荐阅读