1. 程式人生 > >typescript-koa-postgresql 實現一個簡單的rest風格服務器 —— typescript 開發環境配置

typescript-koa-postgresql 實現一個簡單的rest風格服務器 —— typescript 開發環境配置

cit yarn 文件 pos serve 代碼 esc 服務 run

最近需要用 nodeJS 寫一個後臺程序,為了能夠獲得 IDE 的更多代碼提示,決定用 typescript 來編寫,隨便也學習下 ts,在這記錄下實現過程。

1、新建文件夾 typescript-koa-postgresql,初始化項目

yarn init -y

2、安裝 typescript

yarn add typescript @types/node --dev

3、配置 typescript 編譯環境,在項目根目錄下新建文件 tsconfig.json

 1 {
 2   "compilerOptions": {
 3     "target": "es2017",
 4     "outDir": "./dist",
5 "module": "commonjs", 6 "emitDecoratorMetadata": true, 7 "experimentalDecorators": true, 8 "lib": [ 9 "es6" 10 ], 11 "noImplicitAny": false, 12 "sourceMap": false, 13 "allowJs": true 14 }, 15 "include": [ 16 "./src/**/*" 17 ], 18 "exclude": [ 19 "node_modules" 20
] 21 }

4、測試 typescript 環境,新文件夾 src 並添加文件 server.ts

console.log("Hello TypeScript");

在 package.json 中加入

"scripts": {
     "build": "tsc"
 }

運行

yarn run build
node ./dist/server.js

輸出

Hello TypeScript

至此 typescript 環境 配置完成

目錄結構如下:

技術分享圖片

typescript-koa-postgresql 實現一個簡單的rest風格服務器 —— typescript 開發環境配置