npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xz-try-catch

v1.0.8

Published

这是一个方法,封装router路由、db数据库mysql,捕获报错方法

Downloads

51

Readme

[TOC]

xz-try-catch自定义路由router数据库mysql监听报错异常模块

基本的响应数据结构

//成功success对象
success: {
    type: 'success',
    code: '0000',
    data: null,
    message: '已经返回数据!',
    info: null,
    success: true
},
//错误err对象
err: {
    type: 'err',
    code: '0001',
    data: null,
    message: '服务器内部错误!',
    info: null,
    url: '',
    success: false
},

暂时不支持情况-有写明情况可能会捕获报错异常失效

异步阻塞async await

async ()=>{
    console.log(a)//错误代码
	let data = await url().catch(e=>{});//错误代码
}

//提供$router错误响应数据方法
async ()=>{
    try {
    	//错误代码
    	console.log(a)//错误代码
	}catch(e) {
    	//响应错误数据
		$router.$sendErr()
	}
}

异步请求new Promise

return new Promise((resolve, reject) => {
	console.log(a)//错误代码
})


//提供router_tryCatch错误捕获调用
return new Promise((resolve, reject) => {
	$router.tryCatch(()=>{
    	//错误代码
		console.log(a)                
	})
})

使用xz-try-catch模块,框架$express路由router数据库mysql

安装xz-try-catch模块

npm i xz-try-catch//命令1

npm i xz-try-catch --save-dev//命令2,如果命令1不生效

创建一个全局文件tryCatch.js

//导入tryCatch-npm模块
const tryCatch = require('xz-try-catch')


//创建配置$router,$mysql
//封装路由router数据库mysql监听报错异常模块
const tryCatch = tryCatch({
    //路由返回错误数据
    router: {
        mergeQuoteErr: {
            "0001": {code:'0001',info:'internal.code.error',message: '设置的内容 - 服务器内部错误!'},//执行路由内部代码错误
            catch: true//是否输出错误信息
        }
    },
    //数据库返回错误数据
    mysql: {
        mergeQuoteErr: {
            "0001": {code:'0001',info:'db.error success.false',message: '设置的内容 - 服务器内部错误!'},//数据库连接失败/执行语句失败
            // "0002": {code:'0002',info:'db.error internal.code.error',message: '服务器内部错误!'},//执行语句内部代码错误
            // "0003": {code:'0003',info:'db.error',message: '系统繁忙!'},//执行语句影响行成功数0
            catch: true//是否输出错误信息
        },
        createPool: {
            host: '地址',
            user: '用户名',
            password: '密码',
            database: '库名',
            // 还可以添加其他配置,如连接限制、连接超时等
            connectionLimit: 10,
            waitForConnections: true,
            queueLimit: 0
        }
    }
})


//返回路由
module.exports = tryCatch

配置app.js,$express代替express

//全局文件tryCatch.js
const { $express,$router } = require('tryCatch.js')
const app = $express();


//设置路由规则 跨域处理中间件函数
app.use((req,res,next)=>{
  // 设置允许跨域访问的域名,这里设置为 * 表示允许所有域名
  res.setHeader('Access-Control-Allow-Origin', '*');

  // 设置允许的请求方法
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST');

  // 设置允许的请求头
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');

  // 处理 GET /api/data 请求的逻辑
  // res.send('This is route 1');

  //放行
  next()
})

//设置空路由$router
app.use($router((req,res,next)=>{
    
},(err)=>{
    
}))

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

配置$router,$mysql

//全局文件tryCatch.js
const { $router, $mysql } = require('tryCatch.js')
//创建独立路由
const router = $router.Router()


使用router,$mysql
//$router模块
router.get('/get_user',(req,res)=>{
    //错误路由
    console.log(a)
    //使用$send()响应
    res.$send('get_user - Hello, World!');
},(err)=>{

})

router.post('/get_title',(req,res)=>{
    //正确路由
    //$db模块
    $mysql.select('SELECT * FROM member', [], (succ)=>{ 
        res.$send(succ.result);
    },(err)=>{

    });
})


//返回路由
module.exports = router.router

单独使用mysql

//全局文件tryCatch.js
const { $mysql } = require('tryCatch.js')

const index = {
        verificationRegister_email_code(data={}) {
                $db.select('select * from email_code where email=? and email_code=? ORDER BY id DESC LIMIT 1', [email, email_code], (succ) => {
                   	return succ.result
                })
            })
        }
}

//使用
let index = require('路径')
indexverificationRegister_email_code({})

$router说明

//全局文件tryCatch.js
const { $router } = require('tryCatch.js')
//创建独立路由
const router = $router.Router()

//设置空路由$router
//(req,res,next)=>{}
app.use($router((req,res,next)=>{
    
},(err)=>{
    
}))

//get
router.get('/get_user',(req,res,next)=>{

},(err)=>{
	//错误信息
})

//post
router.post('/get_title',(req,res,next)=>{

},(err)=>{
	//错误信息
})

//提供router_tryCatch错误捕获调用
$router.tryCatch(()=>{
    //错误代码
	console.log(a)                
})
//错误响应数据方法
try {
    //错误代码
    console.log(a)
}catch(e) {
    //响应错误数据
	$router.$sendErr()
}

$mysql说明

//全局文件tryCatch.js
const { $mysql } = require('tryCatch.js')

//第一种方式,字符串mysql语句
let mysql = 'select * from email_code where email=? and email_code=? ORDER BY id DESC LIMIT 1'
//第二种方式,对象语句
let mysql = {
	//type=string 语句前缀 select * from user
	prefix = 'select * from user'

	//type=string 搜索用户条件 username账号 and email邮箱
	where = ‘username=? and email=?’

	//type=string 排序 desc降序,asc升序
	order_by = ‘id asc’

	//type=string 查询条数 设置限制比如1
	limit = ‘1’

	//type=string 开始条数 设置从第10条开始 10
	offset = '10'

	//type=array 过滤字段名称 设置过滤字段名
	value_name = ['nameuser','email]
}
//mysql语句?占位符填充数据
let value = ['username', 'passowrd']


$mysql.select(mysql, value, (succ) => {

},(err)=>{
	//错误信息
})