All Projects → soraping → Koa Ts

soraping / Koa Ts

koa2+typescript

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Koa Ts

Blog Service
blog service @nestjs
Stars: ✭ 188 (+129.27%)
Mutual labels:  mongodb, koa, koa2
Koahub Demo
koahub+async/await+mysql
Stars: ✭ 15 (-81.71%)
Mutual labels:  koa, koa2
Mysrv
Yet another Node.js web framework, based on koa.js 又一个 Node.js MVC 框架,基于Koa2
Stars: ✭ 10 (-87.8%)
Mutual labels:  koa, koa2
Docker Vue Node Nginx Mongodb Redis
🐉 An awesome boilerplate, Integrated Docker, Vue, Node, Nginx, Mongodb and Redis in one, Designed to develop & build your web applications more efficient and elegant.
Stars: ✭ 34 (-58.54%)
Mutual labels:  mongodb, koa2
Koa Dec Router
An ES6 decorator + class based router, support inherit, override, priority, auto load controllers, etc.
Stars: ✭ 19 (-76.83%)
Mutual labels:  koa, koa2
Cykspace Node
博客后台服务~~ 👉👉 http://www.cykspace.com
Stars: ✭ 23 (-71.95%)
Mutual labels:  koa, koa2
Smarthome
💡 智能电器管理综合系统
Stars: ✭ 33 (-59.76%)
Mutual labels:  mongodb, koa2
Koa Passport
Passport middleware for Koa
Stars: ✭ 748 (+812.2%)
Mutual labels:  koa, koa2
Vue Socket.io Chat
💬 TypeScript + Vue + Express/Koa + Socket.io
Stars: ✭ 61 (-25.61%)
Mutual labels:  koa, koa2
Koatty
Koa2 + Typescript = Koatty. Use Typescript's decorator implement IOC and AOP.
Stars: ✭ 67 (-18.29%)
Mutual labels:  koa, koa2
Record
✨✨都是自己输出和看过觉得不错的文章,欢迎star、watch!!同时欢迎推荐新文章、书籍和视频!!
Stars: ✭ 69 (-15.85%)
Mutual labels:  mongodb, koa2
Nodeclub Koa
use koa to rewrite nodeclub
Stars: ✭ 18 (-78.05%)
Mutual labels:  koa, koa2
Vue Chat
👥Vue全家桶+Socket.io+Express/Koa2打造一个智能聊天室。
Stars: ✭ 887 (+981.71%)
Mutual labels:  koa, koa2
Koa Generator
Koa' application generator for 1.x and 2.x( Express-style and support all middlewares include async/await )
Stars: ✭ 929 (+1032.93%)
Mutual labels:  koa, koa2
Koa Weixin Jssdk
koa weixin jssdk middleware
Stars: ✭ 7 (-91.46%)
Mutual labels:  koa, koa2
Project16 B Account Book
👥👤👥뭐야..👤👥👥👤👥👤 이거쓰면 부자된다고..?👥👤👥👤👤👥👥 웅성웅성..👤👥👤👥👤궁금하다..👥👥👤👥
Stars: ✭ 28 (-65.85%)
Mutual labels:  mongodb, koa
Koach Javascript
Production ready Koa2 boilerplate.
Stars: ✭ 79 (-3.66%)
Mutual labels:  koa, koa2
Vue Koa Demo
🔰A simple full stack demo(CSR & SSR & Docker Support) written by Vue2 & Koa2(Koa1 verson also completed)
Stars: ✭ 730 (+790.24%)
Mutual labels:  koa, koa2
Node Typescript Koa Rest
REST API boilerplate using NodeJS and KOA2, typescript. Logging and JWT as middlewares. TypeORM with class-validator, SQL CRUD. Docker included. Swagger docs, actions CI and valuable README
Stars: ✭ 739 (+801.22%)
Mutual labels:  koa, koa2
Vue Element Responsive Demo
基于 Vue + Element 的响应式后台模板
Stars: ✭ 54 (-34.15%)
Mutual labels:  mongodb, koa2

koa+typescript

新项目,更加轻量,更加简单,请移步 lenneth

项目下载安装模块

    git clone https://github.com/soraping/koa-ts.git
    cd koa-ts
    npm install

vscode debug 配置

在.vscode 文件夹中,修改 launch.json 文件

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "koa2",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/src/index.ts",
            "stopOnEntry": false,
            "args": [],
            "cwd": "${workspaceRoot}",
            "outFiles": ["${workspaceRoot}/build/**/*.js"],
            "runtimeExecutable": "nodemon",
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "externalConsole": false,
            "sourceMaps": true,
            "restart": true
        }
    ]
}

注意,在属性'runtimeExecutable'处我选用 nodemon 检测 ts 编译后的 js 文件,‘restart’属性选择 true 根目录下 nodemon.json 文件就是 nodemon 的配置文件,要配置所要检测的文件夹,否则会默认检测这个项目,影响性能

tsconfig.json 文件文件配置

{
    "compileOnSave": false,
    "compilerOptions": {
        "module": "commonjs",       // 模块引入方式
        "target": "ES6",            // 将ts文件转成es6的js代码
        // 当 noImplicitAny 标志为 true 的时候,TypeScript 编译器不能推断类型,它仍然生成 JavaScript 文件,但是报告一个错误。
        "noImplicitAny": true,
        "sourceMap": true,          // 开启sourceMap
        "preserveConstEnums": true,
        "emitDecoratorMetadata": true,   // 开启修饰器
        "experimentalDecorators": true,  // 开启修饰器
        "removeComments": true,
        "rootDir": "./src",   // tsc目标文件夹
        "outDir": "./build"   // tsc输出文件夹
    },
    // atom
    "atom": {
        "rewriteTsconfig": true
    },
    // 不需要监控编译的目录
    "exclude": [
        "node_modules",
        "build"
    ],
    // 需要ts编译的文件集合
    "include": [
        "./src/typdts/*/*.d.ts",
        "./src/apis/*.ts"
    ],
    // 需要ts编译的文件绝对路径
    "files": [
        "./src/index.ts"
    ],
    "filesGlob": [
        "./src/**/*.ts"
    ]
}

项目配置文件

// config.json
{
    "port": "8083",          // 端口
    "log": {
        "log_name": "log",   // 日志名称
        "log_path": "logs/"  // 日志路径
    },
    "mongo": {
        "development": {
            "host": "mongodb://localhost:27017/ts-test"
        },
        "production": {
            "host": ""
        }
    },
    "session": {
        "secrets": "koa-ts"
    },
    "jwt": {
        "secret": "koa-ts-jwt"
    }
}

开发

参照了网上的一些案例,进行了整理,利用 es7 的 Decorator 把 koa-router 做了一些封装

import {router, required, prefix, convert, log} from '../middleware/router';
// api path
@prefix('/user')
class UserController{

    // 访问路径就是/user/findOne/zhangsan
    @router({
        method: 'get',
        path: '/findOne/:username',
        unless: true   //如果不做jwt校验,则把这个字段置为true,默认校验
    })
    // 必传参数 {params: ...,query:...}
    @required({params: 'username'})
    // 中间件,api执行前会调用someFun方法
    @convert(someFun)
    // 日志
    @log
    async getUserOne (ctx: IRouterContext): Promise<void> {
        let user = await UserModel.findOne({username: ctx.params.username});
        ctx.body = user;
    }
}

设计 api 时可以按照上述写法,这样写就更加的优雅

启动

首先要确认 mongodb 是开启状态

npm run build  # 启动tsc编译监控

点击 vscode 默认 debug 键, enjoy!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].