njs typescript 开发说明

njs 官方是内置了typescript types 定义的,只是没有发布到npm 官方,目前有几个别人发布的
我自己也构建发布了一个

参考使用

使用typescript 开发一个内容提供服务

  • typescript 配置
 
{
  "include": [
    "./src",
  ],
  "files": [
    "./node_modules/@dalongrong/njs-types/ngx_http_js_module.d.ts",
  ],
  "compilerOptions": {
    "outDir": "./lib",
    "target": "ES5",
    "module": "es2015",                                /* Specify what module code is generated. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  }
}
  • package.json
{
  "name": "njs-ts-apps",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@dalongrong/njs-types": "^0.7.2"
  },
  "scripts": {
    "app":"tsc"
  }
}
  • 开发的模块应用
    src/app.ts
 
function demo(r:NginxHTTPRequest){
    r.headersOut['content-type'] = 'text/plain';
    r.return(200, "Hello from TypeScript");
}
export default {demo}
  • 构建
yarn app
  • docker 集成
version: '3'
services:
  app:
    image: dalongrong/openresty-tengine:debug-perf
    volumes:
      - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
      - "./css/:/usr/local/openresty/nginx/html/"
      - "./lib/app.js:/usr/local/openresty/nginx/html/app.js"
    privileged: true
    cap_add:
       - ALL
    ports:
      - "80:80"
  app2:
    image: dalongrong/openresty-tengine:latest
  app3:
    image: dalongrong/openresty-tengine:latest

效果

 

 

说明

更多关于njs 类型的使用可以参考官方文档,typescript+njs nginx 的扩展性还是很不错的

参考资料

https://github.com/rongfengliang/njs-types
https://www.npmjs.com/package/@dalongrong/njs-types
https://github.com/rongfengliang/openresty-njs-typescript
https://nginx.org/en/docs/njs/reference.html
https://nginx.org/en/docs/njs/typescript.html

posted on 2022-04-09 20:43  荣锋亮  阅读(116)  评论(0编辑  收藏  举报

导航