nodejs 中 swagger-jsdoc 设置的语法错误
Syntax error on swagger-jsdoc setup in nodejs
我在尝试使用我的 express 应用设置 swagger 时遇到语法错误
我试图按照这里的教程 https://itnext.io/setting-up-swagger-in-a-node-js-application-d3c4d7aa56d4 作为快速入门指南,但我不知道为什么会出现该错误
这是我的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;
这是我在 ./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);
};
还有其中一条路线,例如./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;
我按照说明进行操作,我的应用程序应该可以运行,但我收到 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)
错误
在 app.js 中,将 definitions
(复数)更改为 definition
(单数)。 swagger-jsdoc expects 包含 API 定义的选项被命名为 options.swaggerDefinition
或 options.definition
.
const definition = { // <-----
...
};
const options = {
definition, // <-----
apis: ['./routes/index.js'],
};
我在尝试使用我的 express 应用设置 swagger 时遇到语法错误
我试图按照这里的教程 https://itnext.io/setting-up-swagger-in-a-node-js-application-d3c4d7aa56d4 作为快速入门指南,但我不知道为什么会出现该错误
这是我的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;
这是我在 ./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);
};
还有其中一条路线,例如./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;
我按照说明进行操作,我的应用程序应该可以运行,但我收到 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)
错误
在 app.js 中,将 definitions
(复数)更改为 definition
(单数)。 swagger-jsdoc expects 包含 API 定义的选项被命名为 options.swaggerDefinition
或 options.definition
.
const definition = { // <-----
...
};
const options = {
definition, // <-----
apis: ['./routes/index.js'],
};