commit
ec9b507335
9 changed files with 3993 additions and 0 deletions
@ -0,0 +1,133 @@ |
|||
# ---> Node |
|||
# Logs |
|||
logs |
|||
*.log |
|||
npm-debug.log* |
|||
yarn-debug.log* |
|||
yarn-error.log* |
|||
lerna-debug.log* |
|||
.pnpm-debug.log* |
|||
|
|||
# Diagnostic reports (https://nodejs.org/api/report.html) |
|||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json |
|||
|
|||
# Runtime data |
|||
pids |
|||
*.pid |
|||
*.seed |
|||
*.pid.lock |
|||
|
|||
# Directory for instrumented libs generated by jscoverage/JSCover |
|||
lib-cov |
|||
|
|||
# Coverage directory used by tools like istanbul |
|||
coverage |
|||
*.lcov |
|||
|
|||
# nyc test coverage |
|||
.nyc_output |
|||
|
|||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) |
|||
.grunt |
|||
|
|||
# Bower dependency directory (https://bower.io/) |
|||
bower_components |
|||
|
|||
# node-waf configuration |
|||
.lock-wscript |
|||
|
|||
# Compiled binary addons (https://nodejs.org/api/addons.html) |
|||
build/Release |
|||
|
|||
# Dependency directories |
|||
node_modules/ |
|||
jspm_packages/ |
|||
|
|||
# Snowpack dependency directory (https://snowpack.dev/) |
|||
web_modules/ |
|||
|
|||
# TypeScript cache |
|||
*.tsbuildinfo |
|||
|
|||
# Optional npm cache directory |
|||
.npm |
|||
|
|||
# Optional eslint cache |
|||
.eslintcache |
|||
|
|||
# Optional stylelint cache |
|||
.stylelintcache |
|||
|
|||
# Microbundle cache |
|||
.rpt2_cache/ |
|||
.rts2_cache_cjs/ |
|||
.rts2_cache_es/ |
|||
.rts2_cache_umd/ |
|||
|
|||
# Optional REPL history |
|||
.node_repl_history |
|||
|
|||
# Output of 'npm pack' |
|||
*.tgz |
|||
|
|||
# Yarn Integrity file |
|||
.yarn-integrity |
|||
|
|||
# dotenv environment variable files |
|||
.env |
|||
.env.development.local |
|||
.env.test.local |
|||
.env.production.local |
|||
.env.local |
|||
|
|||
# parcel-bundler cache (https://parceljs.org/) |
|||
.cache |
|||
.parcel-cache |
|||
|
|||
# Next.js build output |
|||
.next |
|||
out |
|||
|
|||
# Nuxt.js build / generate output |
|||
.nuxt |
|||
dist |
|||
node_modules |
|||
|
|||
# Gatsby files |
|||
.cache/ |
|||
# Comment in the public line in if your project uses Gatsby and not Next.js |
|||
# https://nextjs.org/blog/next-9-1#public-directory-support |
|||
# public |
|||
|
|||
# vuepress build output |
|||
.vuepress/dist |
|||
|
|||
# vuepress v2.x temp and cache directory |
|||
.temp |
|||
.cache |
|||
|
|||
# Docusaurus cache and generated files |
|||
.docusaurus |
|||
|
|||
# Serverless directories |
|||
.serverless/ |
|||
|
|||
# FuseBox cache |
|||
.fusebox/ |
|||
|
|||
# DynamoDB Local files |
|||
.dynamodb/ |
|||
|
|||
# TernJS port file |
|||
.tern-port |
|||
|
|||
# Stores VSCode versions used for testing VSCode extensions |
|||
.vscode-test |
|||
|
|||
# yarn v2 |
|||
.yarn/cache |
|||
.yarn/unplugged |
|||
.yarn/build-state.yml |
|||
.yarn/install-state.gz |
|||
.pnp.* |
|||
|
@ -0,0 +1,40 @@ |
|||
const Koa = require('koa'); |
|||
const app = new Koa(); |
|||
require('console-color-mr'); //颜色模块
|
|||
const koaBody = require('koa-body'); //处理post请求参数
|
|||
app.use(koaBody({ |
|||
multipart: true, // 支持文件上传
|
|||
})); |
|||
|
|||
// 配置解析请求中间件
|
|||
const autoUpdateRouter = require('./routers/auto/AutoUpdateRouter'); |
|||
app.use(autoUpdateRouter.routes()); |
|||
app.use(autoUpdateRouter.allowedMethods()); |
|||
|
|||
// 字符串转换
|
|||
const json = require('koa-json'); |
|||
app.use(json()); |
|||
|
|||
// 捕获错误logger 记录
|
|||
const logsUtil = require('./utils/LogUtil'); |
|||
app.on('error', (err, ctx) => { |
|||
logsUtil.logError(ctx, err); |
|||
}); |
|||
|
|||
// 捕获全局请求不存在的接口返回404
|
|||
app.use(async (ctx, next) => { |
|||
await next(); |
|||
if (parseInt(ctx.status) === 404) { |
|||
ctx.response.status = 404; |
|||
ctx.body = '404' |
|||
} else if (parseInt(ctx.status) === 500) { |
|||
ctx.response.status = 500; |
|||
ctx.body = "500" |
|||
} |
|||
}) |
|||
|
|||
// 启动服务器
|
|||
const port = 3000; |
|||
app.listen(port, () => { |
|||
console.info(`Server is running on port ${port}`); |
|||
}); |
@ -0,0 +1,81 @@ |
|||
let path = require('path'); |
|||
|
|||
//日志输出目录
|
|||
let baselogPath = path.resolve(__dirname,'../logs'); |
|||
/** |
|||
* 报错输出日志 |
|||
*错误日志目录,文件名,输出完整路径 |
|||
*/ |
|||
let errorPath = '/error' ; |
|||
let errorFileName = 'error'; |
|||
let errorLogPath = baselogPath + errorPath + '/' + errorFileName; |
|||
|
|||
/** |
|||
* 请求数据响应时输出响应日志 |
|||
* 响应日志目录,文件名,输出完整路径 |
|||
*/ |
|||
let responsePath = '/respone'; |
|||
let responseFlieName = 'response' ; |
|||
let responseLogPath = baselogPath + responsePath + '/' +responseFlieName ; |
|||
|
|||
/** |
|||
* 操作数据进行增删改等敏感操作记录 |
|||
* 操作日志目录,文件名,输出完整路径 |
|||
*/ |
|||
let handlePath = '/handle'; |
|||
let handleFlieName = 'handle'; |
|||
let handleLogPath = baselogPath + handlePath + '/' +handleFlieName ; |
|||
|
|||
|
|||
/** |
|||
* 日志格式等设置 |
|||
*/ |
|||
module.exports = { |
|||
//日志格式等设置
|
|||
appenders: |
|||
{ |
|||
"rule-console": {"type": "console"}, // 会打印到控制台
|
|||
"errorLogger": { |
|||
"type": "dateFile", |
|||
"filename": errorLogPath, //日志保存文件的路径及文件名,./为项目根目录
|
|||
"pattern": "yyyy-MM-dd.log", //滚动日志的时间类型,默认为 .yyyy-MM-dd
|
|||
"alwaysIncludePattern": true, //在当前日志文件名中和滚动日志一样包括pattern
|
|||
"encoding": "utf-8", |
|||
"maxLogSize": 52428800, //日志文件的最大大小(以字节为单位),如果未指定,则不会发生日志滚动
|
|||
"numBackups": 3, |
|||
"path": errorPath, |
|||
compress:true |
|||
}, |
|||
"resLogger": { |
|||
"type": "dateFile", |
|||
"filename": responseLogPath, |
|||
"pattern": "yyyy-MM-dd.log", |
|||
"alwaysIncludePattern": true, |
|||
"encoding": "utf-8", |
|||
"maxLogSize": 52428800, |
|||
"numBackups": 3, |
|||
"path": responsePath, |
|||
compress:true |
|||
}, |
|||
"handleLogger": { |
|||
"type": "dateFile", |
|||
"filename": handleLogPath, |
|||
"pattern": "yyyy-MM-dd.log", |
|||
"alwaysIncludePattern": true, |
|||
"encoding": "utf-8", |
|||
"maxLogSize": 52428800, |
|||
"numBackups": 3, |
|||
"path": responsePath, |
|||
compress:true |
|||
}, |
|||
}, |
|||
//供外部调用的名称和对应设置定义
|
|||
categories: { |
|||
"default": {"appenders": ["rule-console"], "level": "all"}, |
|||
"resLogger": {"appenders": ["resLogger"], "level": "info"}, |
|||
"errorLogger": {"appenders": ["errorLogger"], "level": "error"}, |
|||
"handleLogger": {"appenders": ["handleLogger"], "level": "all"}, |
|||
"http": {"appenders": ["resLogger"], "level": "info"} |
|||
}, |
|||
"baseLogPath": baselogPath |
|||
} |
@ -0,0 +1,14 @@ |
|||
|
|||
const autoUpdate = require ("../sdk/ts/autoUpdateMain"); |
|||
|
|||
//推送素材更新方法
|
|||
const pushMaterial = async (ctx, next) => { |
|||
console.log('ctx', ctx.query); |
|||
console.log(ctx.query.req); |
|||
//线上执行更新
|
|||
autoUpdate.readArgv(ctx.query.req); |
|||
}; |
|||
|
|||
const materialAutoUpdate = module.exports = { |
|||
pushMaterial: pushMaterial |
|||
}; |
@ -0,0 +1,19 @@ |
|||
{ |
|||
"restartable": "rs", |
|||
"ignore": [ |
|||
".git", |
|||
".svn", |
|||
"node_modules/**/node_modules" |
|||
], |
|||
"verbose": true, |
|||
"execMap": { |
|||
"js": "node --harmony" |
|||
}, |
|||
"watch": [ |
|||
|
|||
], |
|||
"env": { |
|||
"NODE_ENV": "development" |
|||
}, |
|||
"ext": "js" |
|||
} |
File diff suppressed because it is too large
@ -0,0 +1,27 @@ |
|||
{ |
|||
"name": "design_node_koa", |
|||
"version": "1.0.0", |
|||
"description": "node server", |
|||
"main": "app.js", |
|||
"scripts": { |
|||
"start": "node ./app", |
|||
"dev": "nodemon ./app" |
|||
}, |
|||
"author": "", |
|||
"license": "ISC", |
|||
"dependencies": { |
|||
"koa": "^2.13.0", |
|||
"koa-body": "^4.2.0", |
|||
"koa-helmet": "^6.0.0", |
|||
"koa-json": "^2.0.2", |
|||
"koa-router": "^10.0.0", |
|||
"koa2-cors": "^2.0.6", |
|||
"log4js": "^6.3.0", |
|||
"md5": "^2.3.0", |
|||
"moment": "^2.29.1", |
|||
"nodemon": "^2.0.6" |
|||
}, |
|||
"devDependencies": { |
|||
"console-color-mr": "^2.0.1" |
|||
} |
|||
} |
@ -0,0 +1,11 @@ |
|||
const Router = require('koa-router'); |
|||
const router = new Router({ |
|||
prefix: '/auto' //统一加个前缀
|
|||
}); |
|||
const materialAutoUpdate = require('../../modules/auto/MaterialAutoUpdate'); |
|||
|
|||
// 推送素材更新
|
|||
router.get('/pushMaterial',materialAutoUpdate.pushMaterial); |
|||
|
|||
module.exports = router; |
|||
|
@ -0,0 +1,116 @@ |
|||
let log4js = require('log4js'); |
|||
let logConfig = require('../config/log'); |
|||
|
|||
// 加载配置文件
|
|||
log4js.configure(logConfig); |
|||
|
|||
// 调用预先预定的日志名称
|
|||
let resLogger = log4js.getLogger("resLogger");//请求数据响应
|
|||
let errorLogger = log4js.getLogger("errorLogger"); //错误日志
|
|||
let handleLogger = log4js.getLogger("handleLogger"); //操作数据
|
|||
let consoleLogger = log4js.getLogger(); |
|||
// 格式化日志文本 加上日志头尾和换行方便查看 ==> 普通日志、请求日志、响应日志、操作日志、错误日志
|
|||
let formatText = { |
|||
info: function(info) { |
|||
let logText = new String(); |
|||
//响应日志头信息
|
|||
logText += "\n" + "***************info log start ***************" + "\n"; |
|||
//响应内容
|
|||
logText += "info detail: " + "\n" + JSON.stringify(info) + "\n"; |
|||
//响应日志结束信息
|
|||
logText += "*************** info log end ***************" + "\n"; |
|||
return logText; |
|||
}, |
|||
request: function(req,postObj) { |
|||
let logText = new String(); |
|||
let method = req.method; |
|||
//访问方法
|
|||
logText += "请求方式: " + method + "\n"; |
|||
//请求原始地址
|
|||
logText += "请求接口路径: " + req.originalUrl + "\n"; |
|||
//客户端ip
|
|||
logText += "访问的ip " + req.ip + "\n"; |
|||
//请求的设备
|
|||
logText += "访问的设备: " + req.headers['user-agent'] + "\n"; |
|||
//请求参数
|
|||
if (method === 'GET') { |
|||
logText += "地址栏传的值: " + JSON.stringify(req.query) + "\n"; |
|||
|
|||
}else if(method === 'POST') { |
|||
logText += "POST页面传的值: " + "\n" + JSON.stringify(postObj) + "\n"; |
|||
|
|||
}else{ |
|||
logText += "页面传的值: " + "\n" + JSON.stringify(req.body) + "\n"; |
|||
} |
|||
return logText; |
|||
}, |
|||
response: function(ctx,postObj) { |
|||
let logText = new String(); |
|||
//响应日志开始
|
|||
logText += "\n" + "*************** response log start ***************" + "\n"; |
|||
//添加请求日志
|
|||
logText += formatText.request(ctx.request,postObj); |
|||
//响应状态码
|
|||
logText += "响应状态码: " + ctx.status + "\n"; |
|||
//响应内容
|
|||
logText += "响应内容: " + "\n" + JSON.stringify(ctx.body) + "\n"; |
|||
//响应日志结束
|
|||
logText += "*************** response log end ***************" + "\n"; |
|||
return logText; |
|||
}, |
|||
handle: function(info,name) { |
|||
let logText = new String(); |
|||
//响应日志开始
|
|||
logText += "\n" + "***************info log start ***************" + "\n"; |
|||
//响应内容
|
|||
logText += "数据库语句: " + "\n" + JSON.stringify(info).replace(/\\n/g, "\n") + "\n"; |
|||
//当前目录
|
|||
logText += "当前目录: " + "\n" + JSON.stringify(name).replace(/\\n/g, "\n") + "\n"; |
|||
//响应日志结束
|
|||
logText += "*************** info log end ***************" + "\n"; |
|||
return logText; |
|||
}, |
|||
error: function(ctx, err) { |
|||
let logText = new String(); |
|||
//错误信息开始
|
|||
logText += "\n" + "*************** error log start ***************" + "\n"; |
|||
//添加请求日志
|
|||
logText += formatText.request(ctx.request); |
|||
//错误名称
|
|||
logText += "err name: " + err.name + "\n"; |
|||
//错误信息
|
|||
logText += "err message: " + err.message + "\n"; |
|||
//错误详情
|
|||
logText += "err stack: " + err.stack + "\n"; |
|||
//错误信息结束
|
|||
logText += "*************** error log end ***************" + "\n"; |
|||
return logText; |
|||
} |
|||
} |
|||
|
|||
module.exports = { |
|||
//封装普通日志
|
|||
logInfo: function(info) { |
|||
if (info) { |
|||
consoleLogger.info(formatText.info(info)); |
|||
} |
|||
}, |
|||
//封装响应日志
|
|||
logResponse: function(ctx,postObj) { |
|||
if (ctx) { |
|||
resLogger.info(formatText.response(ctx,postObj)); |
|||
} |
|||
}, |
|||
//封装操作日志
|
|||
logHandle: function(res,name) { |
|||
if (res) { |
|||
handleLogger.info(formatText.handle(res,name)); |
|||
} |
|||
}, |
|||
//封装错误日志
|
|||
logError: function(ctx, error) { |
|||
if (ctx && error) { |
|||
errorLogger.error(formatText.error(ctx, error)); |
|||
} |
|||
} |
|||
}; |
Loading…
Reference in new issue