昨天是設計資料庫架構與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"]
}
新增src
folder以及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介面,測試後接成功取得回應。