-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from MichealWayne/feat-utils-npm
feat add koa2 template
- Loading branch information
Showing
151 changed files
with
535 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Koa2 template | ||
|
||
nodejs 服务模版——Koa2 | ||
|
||
## 项目运行 | ||
|
||
启动: | ||
|
||
```sh | ||
npm run start | ||
``` | ||
|
||
构建: | ||
|
||
```sh | ||
npm run build | ||
``` | ||
|
||
生产构建: | ||
|
||
```sh | ||
npm run build:prod | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"name": "koa2-template", | ||
"version": "1.0.0", | ||
"port": "5000", | ||
"author": "Wayne", | ||
"description": "koa2 nodejs server template.", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"start": "tsc && node dist/", | ||
"dev": "tsc && set NODE_ENV=development && node dist/", | ||
"build": "rm -rf dist/* && tsc", | ||
"build:prod": "tsc && rm -rf node_modules && npm install --production", | ||
"install:prod": "npm install --production", | ||
"docker:start": "npm install && node dist/" | ||
}, | ||
"keywords": [ | ||
"koa2", | ||
"nodejs" | ||
], | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/koa": "^2.11.6", | ||
"@types/koa-router": "^7.4.1", | ||
"@types/koa2-cors": "^2.0.2", | ||
"@types/log4js": "^2.3.5", | ||
"@types/node": "^12.19.3", | ||
"@types/uuid": "^9.0.1", | ||
"@typescript-eslint/eslint-plugin": "^4.21.0", | ||
"@typescript-eslint/parser": "^4.21.0", | ||
"eslint-config-sonarjs": "^1.0.19", | ||
"eslint-plugin-typescript": "^0.14.0", | ||
"typescript": "^4.0.0" | ||
}, | ||
"dependencies": { | ||
"axios": "^1.2.0", | ||
"koa": "^2.13.0", | ||
"koa-body": "^4.2.0", | ||
"koa-router": "^9.4.0", | ||
"koa2-cors": "^2.0.6", | ||
"log4js": "^6.0.0", | ||
"uuid": "^9.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @author Wayne | ||
* @Date 2023-02-15 15:18:59 | ||
* @LastEditTime 2024-05-17 09:55:55 | ||
*/ | ||
import { join } from 'path'; | ||
|
||
const pkg = require(join(__dirname, '../package.json')); | ||
|
||
export const LOG_INFO = { | ||
// 日志配置 | ||
appenders: { | ||
console: { | ||
type: 'console', | ||
}, | ||
trace: { | ||
type: 'dateFile', | ||
filename: './logs/access', | ||
pattern: '.yyyy.log', | ||
alwaysIncludePattern: true, | ||
'maxLogSize ': 204800, | ||
}, | ||
http: { | ||
type: 'logLevelFilter', | ||
appender: 'trace', | ||
level: 'trace', | ||
maxLevel: 'trace', | ||
}, | ||
info: { | ||
type: 'dateFile', | ||
filename: './logs/info', | ||
encoding: 'utf-8', | ||
pattern: '.yyyy.log', | ||
maxLogSize: 204800, | ||
alwaysIncludePattern: true, | ||
layout: { | ||
type: 'pattern', | ||
pattern: '[%d{ISO8601}][%5p %z %c] %m', | ||
}, | ||
compress: true, | ||
}, | ||
maxInfo: { | ||
type: 'logLevelFilter', | ||
appender: 'info', | ||
level: 'debug', | ||
maxLevel: 'error', | ||
}, | ||
error: { | ||
type: 'dateFile', | ||
filename: './logs/error', | ||
pattern: '.yyyy.log', | ||
maxLogSize: 204800, | ||
encoding: 'utf-8', | ||
alwaysIncludePattern: true, | ||
layout: { | ||
type: 'pattern', | ||
pattern: '[%d{ISO8601}][%5p %z %c] %m', | ||
}, | ||
compress: true, | ||
}, | ||
minError: { | ||
type: 'logLevelFilter', | ||
appender: 'error', | ||
level: 'error', | ||
}, | ||
}, | ||
categories: { | ||
default: { | ||
appenders: ['console', 'http', 'maxInfo', 'minError'], | ||
level: 'all', | ||
}, | ||
}, | ||
}; | ||
|
||
export const isDev = process.env.NODE_ENV === 'development'; | ||
export default { | ||
port: pkg.port, | ||
isDev, | ||
file: { | ||
multipart: true, | ||
maxFileSize: 500 * 1024 * 1024, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @module constant | ||
* @author Wayne | ||
* @Date 2023-02-15 15:18:59 | ||
* @LastEditTime 2024-05-17 09:48:46 | ||
*/ | ||
export const SERVER_NAME = 'koa2-template'; | ||
|
||
export const LOG_METHODS = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']; | ||
|
||
export const CODE_NAME = 'status_code'; | ||
export const MESSAGE_NAME = 'status_msg'; | ||
export const INFODATA_NAME = 'data'; | ||
|
||
export const SERVICE_PROTOCOL = 'http:'; | ||
|
||
export const SUCCESS_CODE = 0; | ||
export const ERROR_CODE = 9999; | ||
|
||
export const TIMEOUT = 20 * 1000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @author Wayne | ||
* @Date 2023-02-15 15:18:59 | ||
* @LastEditTime 2024-05-17 09:49:50 | ||
*/ | ||
import Koa from 'koa'; | ||
|
||
import { nextInstance } from '../types'; | ||
|
||
import { SERVER_NAME } from '../constant'; | ||
|
||
import logger from './logger'; | ||
|
||
/** | ||
* hello world(for healthy test) | ||
*/ | ||
export const HelloWorld = async (ctx: Koa.BaseContext) => { | ||
ctx.body = `hello ${SERVER_NAME}.`; | ||
}; | ||
|
||
export const GetTest = async (ctx: Koa.DefaultContext, next: nextInstance) => { | ||
ctx.body = { | ||
result: 'get', | ||
name: ctx.params.name, | ||
param: ctx.query, | ||
}; | ||
if (ctx.params.prompt) logger.info(`${JSON.stringify(ctx.params)}`); | ||
next(); | ||
}; | ||
|
||
export const PostTest = async (ctx: Koa.DefaultContext, next: nextInstance) => { | ||
ctx.body = { | ||
result: 'post', | ||
name: ctx.params.name, | ||
param: ctx.request.body, | ||
}; | ||
|
||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @author Wayne | ||
* @Date 2023-02-15 15:18:59 | ||
* @LastEditTime 2023-06-25 13:37:59 | ||
*/ | ||
import log4js from 'log4js'; | ||
|
||
import { LOG_METHODS } from '../constant'; | ||
import { globalLog } from '../types'; | ||
import { LOG_INFO, isDev } from '../config'; | ||
|
||
log4js.configure(LOG_INFO); | ||
const _logger: any = log4js.getLogger('index'); | ||
|
||
const _logHoc = | ||
(method: string) => | ||
(info: string, ...args: string[]) => { | ||
if (isDev) { | ||
console.log(info, ...args); | ||
} | ||
_logger[method](info, ...args); | ||
}; | ||
|
||
const logger: globalLog = {}; | ||
LOG_METHODS.forEach((method: string) => { | ||
logger[method] = _logHoc(method); | ||
}); | ||
|
||
export default logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @author Wayne | ||
* @Date 2023-02-15 15:18:59 | ||
* @LastEditTime 2023-05-19 15:51:42 | ||
*/ | ||
import Koa from 'koa'; | ||
import { CODE_NAME, MESSAGE_NAME, SUCCESS_CODE, ERROR_CODE } from '../constant'; | ||
|
||
export function sendSuccess(res: Koa.DefaultContext, extobj = {}) { | ||
const resObj = { | ||
[CODE_NAME]: SUCCESS_CODE, | ||
[MESSAGE_NAME]: 'success', | ||
...extobj, | ||
}; | ||
|
||
res.body = resObj; | ||
return resObj; | ||
} | ||
|
||
export function sendFail(res: Koa.DefaultContext, extobj = {}) { | ||
const resObj = { | ||
[CODE_NAME]: ERROR_CODE, | ||
[MESSAGE_NAME]: 'fail', | ||
...extobj, | ||
}; | ||
|
||
res.body = resObj; | ||
return resObj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @author Wayne | ||
* @Date 2023-02-15 15:18:59 | ||
* @LastEditTime 2024-05-17 09:51:06 | ||
*/ | ||
import Koa from 'koa'; | ||
// import cors from 'koa2-cors'; 跨域处理,只有必要时开启 | ||
|
||
import { SERVER_NAME } from './constant'; | ||
|
||
import config from './config'; | ||
import logger from './controllers/logger'; | ||
|
||
import bodyHandler from './middlewares/bodyHandler'; | ||
import errorHandler from './middlewares/errorHandler'; | ||
|
||
import router from './routes'; | ||
|
||
const app = new Koa(); | ||
|
||
app.use(bodyHandler); | ||
app.use(errorHandler); | ||
// app.use(cors()); | ||
|
||
app.use(router.routes()).use(router.allowedMethods()); | ||
|
||
app.listen(config.port, () => { | ||
logger.info(new Date().toLocaleString()); | ||
logger.info(`Service Loaded(${SERVER_NAME})`); | ||
logger.info('Port', config.port); | ||
}); | ||
|
||
app.on('error', (err, ctx) => { | ||
logger.error('server error', err, ctx); | ||
}); |
17 changes: 17 additions & 0 deletions
17
project-templates/backend/koa2/src/middlewares/bodyHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @author Wayne | ||
* @Date 2023-05-09 20:38:53 | ||
* @LastEditTime 2023-12-29 15:02:17 | ||
*/ | ||
import koaBody from 'koa-body'; | ||
|
||
import config from '../config'; | ||
|
||
const bodyHandler = koaBody({ | ||
multipart: config.file.multipart, | ||
formidable: { | ||
maxFileSize: config.file.maxFileSize, | ||
}, | ||
}); | ||
|
||
export default bodyHandler; |
Oops, something went wrong.