project init

This commit is contained in:
zhangyiming
2020-11-16 14:10:17 +08:00
parent 721b84824c
commit 58f3fcbaf3
41 changed files with 6922 additions and 0 deletions

31
backend/src/app.ts Normal file
View File

@@ -0,0 +1,31 @@
import * as express from "express";
import * as bodyParser from "body-parser";
class App {
public app: express.Application;
constructor() {
this.app = express();
this.config();
};
private config(): void{
//支持json编码的主体
this.app.use(bodyParser.json());
//支持编码的主体
this.app.use(bodyParser.urlencoded({
extended: true
}));
//设置静态访问目录(Swagger)
this.app.use(express.static('public'));
//设置跨域访问
this.app.all('*', (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "content-type");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
}
}
export default new App().app;

View File

@@ -0,0 +1,13 @@
/**
* 测试
* @route GET /getApi/
* @summary 测试
* @group test - 测试
* @returns {object} 200
* @security JWT
* @returns {Error} default - Unexpected error
*/
exports.testGetApi = (req, res) => {
res.json({code:1 , msg: "成功"})
}

43
backend/src/server.ts Normal file
View File

@@ -0,0 +1,43 @@
import app from "./app";
const PORT = 3000;
const expressSwagger = require('express-swagger-generator')(app)
// 引入测试数据
const test = require("./router/api/test")
let options = {
swaggerDefinition: {
info: {
description: 'This is a sample server',
title: 'Swagger',
version: '1.0.0'
},
host: 'localhost:3000',
basePath: '/',
produces: ['application/json', 'application/xml'],
schemes: ['http', 'https'],
securityDefinitions: {
JWT: {
type: 'apiKey',
in: 'header',
name: 'Authorization',
description: ''
}
}
},
route: {
url: '/swagger-ui.html',
docs: '/swagger.json' //swagger文件 api
},
basedir: __dirname, //app absolute path
files: ['./router/api/*.ts'] //Path to the API handle folder
}
expressSwagger(options)
app.get('/getApi', (req, res) => {
test.testGetApi(req, res)
})
app.listen(PORT, () => {
console.log('Swagger文档地址:', `http://localhost:${PORT}`);
})