nodejs插件knex 日志打印
nodejs学习
- 1、express & nodejs & mysql 新建项目
- 2、nodejs尝试登陆接口
- 3、nodejs的前端项目搭建以及登陆接口开发
- 4、前端上传图片formdata格式,后端接口处理
- 5、vue+nodejs双表联动
- 6、nodejs插件knex & 日志打印
- 7、vue3+nodejs基于RSA加密的身份认证
- 前端代码地址(不完全功能)
- 后端代码地址(不完全功能)
knex
npm install -save knex
const {dbconfig} = require('../config/index');
const knex = require('knex')({client: 'mysql',connection: dbconfig,log: {error (message) {console.log('[knex error]', message)}}
})
class Base{//查询all (tableName,where,who){return knex(tableName).where(where).select(...who)}// 新增insert (tableName,params){return knex(tableName).insert(params);}// 更改update (tableName,id, params){return knex(tableName).where('id', '=', id).update(params);}// 删除delete (tableName,id){return knex(tableName).where('id', '=', id).del();}//查询双表selectab(aobj,bobj){return knex({ a:aobj.name, b:bobj.name}).select({auser: `a.${aobj.out}`,bupload: `b.${bobj.out}`}).whereRaw('?? = ??', [`a.${aobj.where}`, `b.${bobj.where}`])}
}
// 测试------start
// let db = new Base()//1、select
// db.all('user',{id:'33'},['name']).then(res=>{// console.log(res);
// }).catch(e=>{// console.log(e);
// })//2、insert
// db.insert('user',{name:'lisa',password:'123456'}).then(res=>{// console.log(res);
// }).catch(e=>{// console.log(e);
// })//3、update
// db.update('user',36,{name:'jenny'}).then(res=>{// console.log(res);
// }).catch(e=>{// console.log(e);
// })// 4、delete
// db.delete('user',35).then(res=>{// console.log(res);
// }).catch(e=>{// console.log(e);
// })// 5、selectab
// db.selectab({name:'user',out:'name',where:'relation'},{name:'upload',out:'name',where:'id'}).then(res=>{// console.log(res);
// }).catch(e=>{// console.log(e);
// })// 测试------end
module.exports = new Base();
node utils/knex.ts
打印日志
npm i winston
根目录添加一个配置打印日志格式的文件 logger.js,文末有github地址,内参考logger.js
const { createLogger, format, transports } = require('winston'); const fs = require('fs'); const path = require('path');const env = process.env.NODE_ENV || 'development'; const logDir = 'log';// Create the log directory if it does not exist if (!fs.existsSync(logDir)) {fs.mkdirSync(logDir); }const filename = path.join(logDir, 'results.log');const logger = createLogger({// change level if in dev environment versus productionlevel: env === 'production' ? 'info' : 'debug',format: format.combine(format.label({ label: path.basename(process.mainModule.filename) }),format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' })),transports: [new transports.Console({format: format.combine(format.colorize(),format.printf(info =>`${info.timestamp} ${info.level} [${info.label}]: ${info.message}`))}),new transports.File({filename,format: format.combine(format.printf(info =>`${info.timestamp} ${info.level} [${info.label}]: ${info.message}`))})] });module.exports = logger;
app.js内请求错误处理的部分删掉,更改为打印日志
const logger = require('./logger') app.use(function(err, req, res, next) {logger.error(`${req.method} ${req.originalUrl} ` + err.message)const errmsg = err.messageres.status(err.status||500).json({code:-1,success:false,message:errmsg,data:{} }) });
参考:
Node.js + Express + MySQL 搭建项目框架
winston
Knex.js
nodejs插件knex 日志打印相关推荐
- Ice飞冰页面配置菜单配置日志打印环境配置《六》
页面配置 框架为页面级组件提供了一些特殊的配置项,让页面级组件可以快速拥有一些能力. 页面滚动# scrollToTop:用于渲染页面前是否需要将当前页面滚动至顶部,这个配置用于嵌套路由的应用场景. ...
- Jenkins基础:使用NPM构建前端应用1: nodejs插件的安装与设定
关于在Jenkins中使用npm的需要,有多种方法,如果可以接受直接在Jenkins上手动地配置与设定的方式,使用名为nodejs的插件则可以实现前端应用的集成. 插件介绍 项目 说明 插件名称 no ...
- mybatis日志打印大杀器
文章目录 前言 MybatisLogFormat 选项一 选项二 缺点 Mybatis Log Free 总结 前言 各位精通CRUD的老司机,相信大家在工作中mybatis或者mybatisplus ...
- java 日志颜色_【Java】+日志打印+日志颜色设置
先看个效果 一.log4j日志打印配置 1.1.maven依赖 log4j log4j 1.2.17 1.2.新建log4j.properties文件 项目src目录下新建一个文件 "log ...
- Spring boot配置logback日志打印到指定路径文件
在实际项目开发中,一般都是使用log打印日志到指定路径,生成文件,而平时使用的System.out.pring()只能输出日志到控制台. 当我们把项目打成war包部署到服务器上,会有不用的用户使用我们 ...
- [pig4cloud框架源码分析] 03 - MyBatis中的sql语句日志打印
文章目录 导读 pig4cloud框架配置 Mybatis Log Plugin 插件开启方式 插件说明 [TODO]源码分析 拦截器方案实现sql日志查看 参考资料 导读 使用MyBatis开发过程 ...
- mybatis-plus 开启与关闭 SQL 日志打印
开启打印 Mybatis-plus 需要通过下面的方式开启控制台 SQL 日志打印 mybatis-plus:configuration:log-impl: org.apache.ibatis.log ...
- SpringBoot+logback实现日志打印
SpringBoot+logback 一.日志简介 1.SLF4J介绍 二.SpringBoot集成logback 1.配置文件详解 1.彩色日志 2.appender 3.root和logger详解 ...
- NodeJS插件验证:Jenkins官方Alpine镜像之LTS版本
在前面的文章中我们验证了Alpine版本的Linux发行版和NodeJS安装包不兼容的情况,并给出了解决的方法.而Jenkins本身也提供Alpine版本的镜像,这篇文章来确认一下目前最新的Alpin ...
最新文章
- 清华成立视觉智能研究中心,邓志东任中心主任
- 高效模式编写者的7个习惯
- 文化之旅(dijstra)
- SQLServer: 无法修改表
- Mock.js 和Node.js详细讲解
- Acess 数据库 查询数据表结构等问题小记
- Kubernetes探索学习005--Kubernetes的Controller模型和ReplicaSet伸缩
- ubuntu Qt Creator不能输入中文
- 外边距的典型应用-让块级盒子水平居中(HTML、CSS)
- 项目是如何完成的(一)
- 实战篇-六十六行完成简洁的Rss输出类
- sql server 用户创建与权限管理
- app图标圆角角度_?APP图标造型分析!
- python 爬虫保存为word_微信公众号文章爬虫,本地word文档保存
- iOS监听键盘的删除按键事件
- Android Tips 8
- 模电数电EDA实验开发系统实验设备QY-MS301D
- 一点资讯推出“长风计划” 内容分发平台进入拉人大战
- 安卓移动软件开发:简易录音软件实现
- 思科AP无线异常及信道相关知识点整理