iT邦幫忙

2023 iThome 鐵人賽

DAY 26
0

Simple Auth App:專案始動

昨天是設計資料庫架構與API介面,今天就來開始啟動專案,這次不用WebStorm建立專案而是使用指令來建立專案。

建立專案

mkdir simple-auth-app
cd simple-auth-app/
npm init
npm install express typescript ts-node @types/node @types/express --save-dev

新增tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "./dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

新增srcfolder以及src/index.ts,然後建立.gitignore

node_modules/

然後編輯package.json加入啟動指令

{
  "name": "simple-auth-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "ts-node src/index.ts",
    "build": "tsc",
    "serve": "node dist/index.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.18",
    "@types/node": "^20.8.4",
    "express": "^4.18.2",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  }
}

然後是在index.ts加入API介面

import express, { Request, Response } from 'express';

const app = express();
const PORT = 3000;

app.use(express.json());

// Sign Up API
app.post('/api/signup', (req: Request, res: Response) => {
    // TODO: Implement signup logic
    res.json({ message: "User registered successfully" });
});

// Email Verification API
app.get('/api/verify-email/:token', (req: Request, res: Response) => {
    // TODO: Implement email verification logic using the provided token
    res.json({ message: "Email verified successfully" });
});

// Login API
app.post('/api/login', (req: Request, res: Response) => {
    // TODO: Implement login logic
    res.json({ message: "User logged in successfully" });
});

// User Profile API
app.get('/api/profile', (req: Request, res: Response) => {
    // TODO: Implement user profile retrieval logic
    res.json({ message: "User profile retrieved successfully" });
});

// Reset Password API
app.post('/api/reset-password', (req: Request, res: Response) => {
    // TODO: Implement password reset logic
    res.json({ message: "Password reset successfully" });
});

// Logout API
app.post('/api/logout', (req: Request, res: Response) => {
    // TODO: Implement logout logic
    res.json({ message: "User logged out successfully" });
});

// User Dashboard API
app.get('/api/dashboard', (req: Request, res: Response) => {
    // TODO: Implement user dashboard retrieval logic
    res.json({ message: "User dashboard retrieved successfully" });
});

app.listen(PORT, () => {
    console.log(`Server is running at http://localhost:${PORT}`);
});

然後在cmd執行,

pnpm install
pnpm start

在postman中新增一個collection跟新增API介面,測試後接成功取得回應。
https://ithelp.ithome.com.tw/upload/images/20231010/20140358bm0hyrjYKn.png


上一篇
[Day 25] Simple Auth App 實戰
下一篇
[Day 27] Simple Auth App:ORM & DB setting
系列文
從實戰中學習:Take Home Assignment review & refactor30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言