nginx njs npm hashids 模块使用

主要是将今天说到的工具做一个使用说明

场景

直接复用npm 的hashids 生成一个id,同时基于rollup 构建,对于缺少js 特性支持的基于core-js 进行polyfill

参考代码

 
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

posted on   荣锋亮  阅读(367)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用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 私有镜像处理

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示