简体   繁体   English

在Node.js中设置swagger-jsdoc的语法错误

[英]Syntax error on swagger-jsdoc setup in nodejs

I am getting a syntax error on trying to set up swagger with my express app 我在尝试通过我的Express应用设置招摇时收到语法错误

I was trying to follow this tutorial here https://itnext.io/setting-up-swagger-in-a-node-js-application-d3c4d7aa56d4 as a quick start guide, I do not know why I am getting that error 我试图在这里按照本教程https://itnext.io/setting-up-swagger-in-a-node-js-application-d3c4d7aa56d4作为快速入门指南,我不知道为什么会收到该错误

This is my app.js 这是我的app.js

import 'regenerator-runtime/runtime';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import swaggerUI from 'swagger-ui-express';
import swaggerJSDocs from 'swagger-jsdoc';
import allRoutes from './routes';


const definitions = {
  info: {
    // API information (required)
    title: 'Wayfarer', // Title (required)
    version: '1.0.0', // Version (required)
  },
  securityDefinitions: {
    bearerAuth: {
      type: 'apiKey',
      name: 'Authorization',
      scheme: 'bearer',
      in: 'header',
    },
  },
};

const options = {
  definitions,
  apis: ['./routes/index.js'],
};

const swaggerSpec = swaggerJSDocs(options);

const app = express();

app.get('/swagger.json', (req, res) => {
  res.setHeader('Content-Type', 'application/json');
  res.send(swaggerSpec);
});
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerSpec));
app.use(cors());
app.use(bodyParser.urlencoded({
  extended: true,
}));
app.use(bodyParser.json());

allRoutes(app);

export default app;

And here are my routes in ./routes/index.js 这是我在./routes/index.js路线

import userRouter from './users';
import busRouter from './buses';
import tripRouter from './trips';

const versionNumber = '/api/v1';

export default (app) => {
  app.use(versionNumber, userRouter);
  app.use(versionNumber, busRouter);
  app.use(versionNumber, tripRouter);
};

And one of the routes eg ./routes/users 以及其中一条路线,例如./routes/users

import router from './router';
import userMiddleware from '../middleware/users';

router.post('/auth/signup', userMiddleware.signupClients(), userController.signUp.bind(userController));

router.post('/auth/signin', userMiddleware.signinClients(), userController.signinClients.bind(userController));

router.post('/auth/signin/admin', userMiddleware.signinAdmin(), userController.signinAdmin.bind(userController));

export default router;

I followed the instructions and my app should work instead I am getting a Error: SyntaxError: Unexpected token u in JSON at position 0 at module.exports (/home/frank/Desktop/Web Dev/Git projects/Way-Farer/node_modules/swagger-jsdoc/lib/index.js:32:11) at Object.<anonymous> (/home/frank/Desktop/Web Dev/Git projects/Way-Farer/api/src/app.js:32:21) error 我按照说明进行操作,我的应用程序应该可以运行,但我收到Error: SyntaxError: Unexpected token u in JSON at position 0 at module.exports (/home/frank/Desktop/Web Dev/Git projects/Way-Farer/node_modules/swagger-jsdoc/lib/index.js:32:11) at Object.<anonymous> (/home/frank/Desktop/Web Dev/Git projects/Way-Farer/api/src/app.js:32:21)错误

In app.js, change definitions (plural) to definition (singular). 在app.js中,将definitions (复数)更改为definition (单数)。 swagger-jsdoc expects that the option containing the API definition be named either options.swaggerDefinition or options.definition . swagger-jsdoc 希望将包含API定义的选项命名为options.swaggerDefinitionoptions.definition

const definition = {  // <-----
  ...
};

const options = {
  definition,      // <-----
  apis: ['./routes/index.js'],
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM