1.4 koa2改造为TS项目

本文介绍了如何搭建一个基于Koa2的Web应用,详细阐述了安装依赖、配置package.json、创建nodemon.json和tslint.json的过程。通过TSLint进行代码规范,使用TypeScript提升类型安全性,并展示了如何将应用转换为TS项目,包括路由、入口文件的调整。最后,提到了nodemon的配置更改和应用的运行测试。

一、安装相关依赖

npm install tslint typescript -g
npm install @types/koa-logger @types/koa-router @types/koa-views -g
npm install nodemon  ts-node -g

二、配置package.json的script

 "scripts": {
    "start": "npm run serve",
    "serve": "nodemon src/bin/www.ts --config nodemon.json",
    "build": "npm run tslint && npm run build-ts",
    "build-ts": "tsc",
    "watch": "npm run tslint && npm run watch-ts",
    "watch-ts": "tsc -w",
    "tslint": "tslint -c tslint.json -p tsconfig.json"
  }

三、根目录创建nodemon.json并配置

{
  "restartable": "rs",
  "verbose": true,
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["node_modules/**/node_modules"],
  "execMap": {
    "js": "node --harmony"
  }
}

四、根目录创建并配置tslint.json

{
  "rules": {
    "class-name": true,
    "comment-format": [
      true,
      "check-space"
    ],
    "indent": [
      true,
      "spaces",
      2
    ],
    "one-line": [
      true,
      "check-open-brace",
      "check-whitespace"
    ],
    "no-var-keyword": true,
    "quotemark": [
      true,
      "single",
      "avoid-escape"
    ],
    "semicolon": [
      true,
      "always",
      "ignore-bound-class-methods"
    ],
    "whitespace": [
      true,
      "check-branch",
      "check-decl",
      "check-operator",
      "check-module",
      "check-separator",
      "check-type"
    ],
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值