|
| 1 | +--- |
| 2 | +title: 創建 express 的 typescript 環境 |
| 3 | +date: 2023-11-18 09:53:50 |
| 4 | +categories: 後端 |
| 5 | +tags: |
| 6 | + - Express |
| 7 | + - Node.js |
| 8 | + - Typescript |
| 9 | +cover: /images/posts/build-express-with-typescript/cover.jpg |
| 10 | +description: 本篇文章將從 Node.js 環境創建,安裝相關套件,並配置 TypeScript,逐步完成一個使用 TypeScript 的 Express 應用。 |
| 11 | +--- |
| 12 | + |
| 13 | +## 基礎 Express 環境 |
| 14 | + |
| 15 | +### 建立 Node 環境 |
| 16 | + |
| 17 | +```bash |
| 18 | +pnpm init |
| 19 | +``` |
| 20 | + |
| 21 | +### 安裝 Express 與相關套件 |
| 22 | + |
| 23 | +```bash |
| 24 | +pnpm i express |
| 25 | +pnpm i -D typescript nodemon ts-node |
| 26 | +``` |
| 27 | + |
| 28 | +- [express](https://expressjs.com/): Node.js 的 Web Framework |
| 29 | +- [nodemon](https://nodemon.io/): 修改代碼後,自動重新啟動的開發工具 |
| 30 | +- [typescript](https://www.typescriptlang.org/): 編譯套件,將 TypeScript 編譯為 JavaScript |
| 31 | +- [ts-node](https://typestrong.org/ts-node/): 可以直接執行 .ts 文件的 Node.js 環境 (開發環境下 nodemon 會使用此套件直接執行 .ts 文件) |
| 32 | + |
| 33 | +### 安裝 Node 與 Express 的型別 |
| 34 | + |
| 35 | +```bash |
| 36 | +pnpm i -D @types/node @types/express |
| 37 | +``` |
| 38 | + |
| 39 | +- @types/node: node 的型別 |
| 40 | +- @types/express: express 的型別 |
| 41 | + |
| 42 | +### 修改 package.json 中 scripts |
| 43 | + |
| 44 | +```json |
| 45 | +"scripts": { |
| 46 | + "dev": "nodemon index.ts", |
| 47 | + "build": "tsc", |
| 48 | + "start": "node ./dist/index.js" |
| 49 | +}, |
| 50 | +``` |
| 51 | + |
| 52 | +### 完整 package.json |
| 53 | + |
| 54 | +```json |
| 55 | +{ |
| 56 | + "name": "express-with-ts", |
| 57 | + "version": "1.0.0", |
| 58 | + "description": "", |
| 59 | + "main": "index.js", |
| 60 | + "scripts": { |
| 61 | + "dev": "nodemon index.ts", |
| 62 | + "build": "tsc", |
| 63 | + "start": "node ./dist/index.js" |
| 64 | + }, |
| 65 | + "keywords": [], |
| 66 | + "author": "", |
| 67 | + "license": "ISC", |
| 68 | + "dependencies": { |
| 69 | + "express": "^4.18.2" |
| 70 | + }, |
| 71 | + "devDependencies": { |
| 72 | + "@types/express": "^4.17.20", |
| 73 | + "@types/node": "^20.8.10", |
| 74 | + "nodemon": "^3.0.1", |
| 75 | + "ts-node": "^10.9.1", |
| 76 | + "typescript": "^5.2.2" |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## 基礎 Express 代碼 |
| 82 | + |
| 83 | +```typescript |
| 84 | +// index.js |
| 85 | +import express from 'express'; |
| 86 | + |
| 87 | +const app = express(); |
| 88 | +const port = 3000; |
| 89 | + |
| 90 | +app.get('/', (req, res) => { |
| 91 | + res.send('hello'); |
| 92 | +}); |
| 93 | + |
| 94 | +app.listen(port, () => { |
| 95 | + console.log(`Example app url: http://localhost:${port}`); |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +此時,可以先執行 `pnpm dev` 看看能不能成功 |
| 100 | + |
| 101 | +```bash |
| 102 | +pnpm dev |
| 103 | +``` |
| 104 | + |
| 105 | +## 配置 TypeScript |
| 106 | + |
| 107 | +### 創建 TypeScript 環境 |
| 108 | + |
| 109 | +執行 `tsc --init` 初始化 TypeScript,此時會自動產生 `tsconfig.json`,裡面已經有預設配置 |
| 110 | + |
| 111 | +```bash |
| 112 | +tsc --init |
| 113 | +``` |
| 114 | + |
| 115 | +### 輸出目錄 |
| 116 | + |
| 117 | +在 tsconfig.json 中搜尋 `outDir` 配置項,可以配置輸出目錄,此範例中我們設定為 `dist` (搭配 `package.json` 中 script 的 start) |
| 118 | + |
| 119 | +```json |
| 120 | +"outDir": "./dist" |
| 121 | +``` |
| 122 | + |
| 123 | +## 延伸閱讀 |
| 124 | + |
| 125 | +如此一來,我們就成功建構了能以 TypeScript 開發的 Express 環境 |
| 126 | + |
| 127 | +在此基礎下,可以去參考我寫的其他文章 |
0 commit comments