在nodejs应用程序上设置swagger时,我遇到了语法错误。我对节点使用了fastify框架。我的配置基于本教程how-to-build-blazing-fast-rest-apis-with-node-js-mongodb-fastify-and-swagger
这是我的server.js:
const swagger = require('./main/config/swagger')
const routes = require('./main/routes/v1')
const config = require('./main/config')
const fastifylogger = {
level: config.logger.level,
file: config.logger.file,
serializers: {
res: (res) => ({
statusCode: res.statusCode,
request: res.input,
payload: res.payload,
}),
},
}
const fastify = require('fastify')({
logger: config.logger.enabled ? fastifylogger : undefined,
pluginTimeout: 1000,
})
fastify.register(require('fastify-swagger'), swagger.options)
fastify.ready(() => {
console.log('fastify server is ready')
fastify.swagger()
})
fastify.listen(config.server.port, '0.0.0.0')这是swagger.js文件:
exports.options = {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Fastify API',
description: 'Building a blazing fast REST API with Node.js, MongoDB, Fastify and Swagger',
version: '1.0.0',
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here',
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json'],
},
}但是,当我转到这个url :localhost:3002/文档来查看我的swagger时,我得到了这个错误:SyntaxError: JSON.parse ()位置0处的JSON中的意外令牌u,我不知道我应该如何调试这个错误,为什么会出现这个错误!
发布于 2019-12-25 10:12:58
我猜comma-dangle
https://eslint.org/docs/rules/comma-dangle
我建议你检查一下你的swagger.js
exports.options = {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Fastify API',
description: 'Building a blazing fast REST API with Node.js, MongoDB, Fastify and Swagger',
version: '1.0.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json']
}
}试试这个。
fastify.register(require('fastify-swagger'), {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Fastify API',
description: 'Building a blazing fast REST API with Node.js, MongoDB, Fastify and Swagger',
version: '1.0.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost',
schemes: ['http'],
consumes: ['application/json'],
produces: ['application/json']
}
});最后一次尝试。
// move .swagger() code
// move under line `listen`
await fastify.listen(3000)
fastify.swagger()
fastify.log.info(`listening on ${fastify.server.address().port}`)https://stackoverflow.com/questions/59477192
复制相似问题