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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2021-04-09 cube.js mssql driver 问题
2019-04-09 使用fpm 方便快速生成postgresql extension分发包
2014-04-09 web性能优化