nginx njs npm hashids 模块使用
主要是将今天说到的工具做一个使用说明
场景
直接复用npm 的hashids 生成一个id,同时基于rollup 构建,对于缺少js 特性支持的基于core-js 进行polyfill
参考代码
- fix hashids 问题
因为原始hahsids 是基于了set ,但是对于set支持有点问题,所以调整了(核心是... spread 的特性支持)
https://github.com/rongfengliang/hashids.js
https://github.com/rongfengliang/hashids.js/blob/master/src/hashids.ts#L61 - 集成hashids 到njs 中
项目使用了njs-typescript-starter 模版,很方便,详细代码参考github
src/index.ts
import 'core-js/actual/array/from';
import 'core-js/actual/set';
import { hello,token } from './hello'
// NOTE: This module must contain only the default export, no named exports!
export default {
hello,
token
}
hello.ts
import qs from 'querystring'
// 使用自己修改的hashids 服务
import hashids from '@dalongrong/hashids'
export function hello (r: NginxHTTPRequest): void {
const name = r.args.name ? qs.unescape(r.args.name) : 'world'
return r.return(200, `
Meow, ${name}!
("\`-''-/").___..--''"\`-._
\`6_ 6 ) \`-. ( ).\`-.__.\`)
(_Y_.)' ._ ) \`._ \`. \`\`-..-'
_..\`--'_..-_/ /--'_.' ,'
(il),-'' (li),' ((!.-'
`)
}
export function token(r: NginxHTTPRequest):void {
let myhashids = new hashids("demoapp",10).encode(Math.floor(Math.random()*4000));
return r.return(200,myhashids)
}
- njs 集成使用
js_import /opt/jsapp/main.js;
location = /token {
js_content main.token;
}
效果
说明
以上是工具集成的一个使用说明,实际上目前njs 还没有openresty 强大,功能上还是比较简单的,但是基于npm 扩展还可以暂时弥补一部分缺少的功能,期待
更加强大
参考资料
https://github.com/jirutka/babel-preset-njs
https://github.com/jirutka/njs-typescript-starter
https://github.com/jirutka/nginx-testing
https://github.com/rongfengliang/hashids.js
https://github.com/rongfengliang/nginx-njs-learning/tree/1.21.5
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2020-05-28 VictoriaMetrics v1.36.0 的一些新功能
2019-05-28 Kapitan 通用terraform&& kubernetes 配置管理工具
2019-05-28 sqler 集成 terraform v0.12 生成资源部署文件
2018-05-28 nexus helm proxy 集成&&问题解决
2018-05-28 nexus yum 私服集成
2018-05-28 nexus bower 集成使用
2018-05-28 nexus docker 私有镜像处理