Typescript 和 Koa 构建 Node 应用(demo)
详细说明:掘金
- 安装项目依赖
npm install
- 安装全局依赖
npm install -g -force ts-node nodemon
- 运行
npm watch-update
或者
nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./src/index.ts
mkdir src
npm init
npm install koa koa-router
npm install --save-dev @types/koa @types/koa-router
npm install --save-dev typescript ts-node nodemon
这里会有个坑(这里使用的是window环境下)就是 ts-node
和 nodemon
这两个需要全局安装才能执行热更新的命令
npm install -g -force ts-node nodemon
"scripts": {
"start": "tsc && node dist/index.js",
"watch-update": "nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./src/index.ts"
},
如果执行不了 npm watch-update
那就执行 nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node' ./src/index.ts
不确定是否 window
环境下的问题还是 npm
的问题,项目首次创建并执行的时候,所有依赖都可以本地安装并且 npm watch-update
也可以完美执行
但是再次打开项目的时候就出错了,目前还没找到原因,不过以上方法可以解决
npm install koa-body