我们在nodeJs应用程序中实现了swagger。到目前为止,我们是创建生产建设使用webpack,并删除控制器和服务文件。
bin/www.js
const YAML = require('yamljs');
const swaggerTools = require('swagger-tools');
const swaggerDoc = YAML.safeLoad('./swagger.yaml');
// swaggerRouter configuration
const swaggerOptions = {
controllers: path.join(__dirname, '../public/javascripts/controllers'),
useStubs: true, // Conditionally turn on stubs (mock mode)
};
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, (middleware) => {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// validate the security using JWT token
app.use(middleware.swaggerSecurity({
Bearer: auth.verifyToken
}));
// Validate Swagger requests
app.use(middleware.swaggerValidator({
validateResponse: true
}));
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(swaggerOptions));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
});如果我们在生产过程中做同样的事情,那么我们就会看到同样的方式来解决这个问题。生成后,我们删除公用文件夹。
Webpack码
const path = require('path');
const nodeExternals = require('webpack-node-externals');
module.exports = {
entry: {
server: './bin/www',
},
output: {
path: path.join(__dirname, 'dist'),
publicPath: '/',
filename: 'server.build.js',
},
target: 'node',
node: {
// Need this when working with express, otherwise the build fails
__dirname: false, // if you don't put this is, __dirname
__filename: false, // and __filename return blank or /
},
externals: [nodeExternals()],
module: {
rules: [
{
// Transpiles ES6-8 into ES5
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
],
},
};请求帮助我们使用swagger中间件创建一个构建。
提前感谢
发布于 2020-09-01 03:51:05
装腔作势的工具不是像webpack那样的包包。因此,您仍然需要向它提供控制器文件。因为您要从prod中删除/public,所以swagger工具中间件无法获得它需要的文件。在这种情况下,Webpack基本上是从代码中构建一个dist,这就是为什么删除控制器和服务是可以的。
https://stackoverflow.com/questions/63517502
复制相似问题