_legacy/backend/backend-development/SKILL.md
バックエンド開発の基礎。API設計、データベース設計、認証・認可、エラーハンドリング、セキュリティなど、堅牢なバックエンドシステム構築のベストプラクティス。
npx skillsauth add gaku52/claude-code-skills backend-developmentInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
このSkillは、バックエンド開発の基礎をカバーします:
このガイドで学べること: API設計パターン、セキュリティ原則、エラーハンドリング戦略 公式で確認すべきこと: 最新セキュリティ脆弱性、OWASP Top 10、フレームワーク最新機能
対象者:プログラミング初心者、バックエンド開発が初めての方
学習時間:約6〜8時間
バックエンド開発の基礎を体系的に学べる7つのガイドを用意しました。順番に学習することで、実践的なAPIを構築できるようになります。
バックエンド開発とは (30〜40分)
HTTP基礎 (30〜40分)
REST API入門 (40〜50分)
データベース基礎 (1〜2時間)
認証の基礎 (1〜2時間)
環境変数と設定管理 (40〜50分)
シンプルなAPI構築 (1〜2時間)
Week 1: 01→02→03(基礎概念)
Week 2: 04→05(データベース・認証)
Week 3: 06→07(実践・統合)
学習後に作れるもの:
以下の完全ガイドで、堅牢なバックエンド構築を学べます:
27,000文字 | Node.js 20.0+ | Express 4.18+ | GraphQL 16.8+
27,000文字 | Winston 3.11+ | Sentry 7.100+
27,000文字 | Helmet 7.1+ | bcrypt 5.1+ | OWASP ZAP 2.14+
合計: 81,000文字 | 3ガイド
GET /api/users # ユーザー一覧取得
GET /api/users/:id # 特定ユーザー取得
POST /api/users # ユーザー作成
PUT /api/users/:id # ユーザー更新
DELETE /api/users/:id # ユーザー削除
GET /api/users/:id/posts # 特定ユーザーの投稿一覧
// ✅ 成功レスポンス(200 OK)
{
"data": {
"id": "123",
"name": "John Doe",
"email": "[email protected]"
}
}
// ✅ リストレスポンス
{
"data": [
{ "id": "1", "name": "User 1" },
{ "id": "2", "name": "User 2" }
],
"meta": {
"total": 100,
"page": 1,
"perPage": 20
}
}
// ✅ エラーレスポンス(400 Bad Request)
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid input",
"details": [
{ "field": "email", "message": "Invalid email format" }
]
}
}
| コード | 説明 | 使用例 | |--------|------|--------| | 200 | OK | 成功(GET, PUT) | | 201 | Created | リソース作成成功(POST) | | 204 | No Content | 削除成功(DELETE) | | 400 | Bad Request | バリデーションエラー | | 401 | Unauthorized | 認証失敗 | | 403 | Forbidden | 権限不足 | | 404 | Not Found | リソースが存在しない | | 500 | Internal Server Error | サーバーエラー |
// トークン生成
import jwt from 'jsonwebtoken'
function generateToken(userId: string) {
return jwt.sign(
{ userId },
process.env.JWT_SECRET!,
{ expiresIn: '7d' }
)
}
// トークン検証
function verifyToken(token: string) {
try {
return jwt.verify(token, process.env.JWT_SECRET!)
} catch (error) {
throw new Error('Invalid token')
}
}
// ミドルウェア
async function authMiddleware(req, res, next) {
const token = req.headers.authorization?.replace('Bearer ', '')
if (!token) {
return res.status(401).json({ error: 'No token provided' })
}
try {
const decoded = verifyToken(token)
req.userId = decoded.userId
next()
} catch (error) {
return res.status(401).json({ error: 'Invalid token' })
}
}
import bcrypt from 'bcrypt'
// パスワードハッシュ化
async function hashPassword(password: string) {
return bcrypt.hash(password, 10)
}
// パスワード照合
async function comparePassword(password: string, hash: string) {
return bcrypt.compare(password, hash)
}
// 使用例
const hashedPassword = await hashPassword('mypassword')
const isValid = await comparePassword('mypassword', hashedPassword)
// ユーザーロール
enum Role {
USER = 'user',
ADMIN = 'admin',
MODERATOR = 'moderator'
}
// 権限チェックミドルウェア
function requireRole(...roles: Role[]) {
return async (req, res, next) => {
const user = await prisma.user.findUnique({
where: { id: req.userId }
})
if (!user || !roles.includes(user.role)) {
return res.status(403).json({ error: 'Forbidden' })
}
next()
}
}
// 使用例
router.delete('/api/users/:id', authMiddleware, requireRole(Role.ADMIN), deleteUser)
// エラークラス定義
class AppError extends Error {
constructor(
public statusCode: number,
public code: string,
message: string,
public details?: any
) {
super(message)
this.name = 'AppError'
}
}
class ValidationError extends AppError {
constructor(message: string, details?: any) {
super(400, 'VALIDATION_ERROR', message, details)
}
}
class NotFoundError extends AppError {
constructor(resource: string) {
super(404, 'NOT_FOUND', `${resource} not found`)
}
}
class UnauthorizedError extends AppError {
constructor(message = 'Unauthorized') {
super(401, 'UNAUTHORIZED', message)
}
}
// Express エラーハンドラー
function errorHandler(err: Error, req, res, next) {
console.error(err)
if (err instanceof AppError) {
return res.status(err.statusCode).json({
error: {
code: err.code,
message: err.message,
details: err.details
}
})
}
// 予期しないエラー
return res.status(500).json({
error: {
code: 'INTERNAL_ERROR',
message: 'An unexpected error occurred'
}
})
}
// 使用例
app.use(errorHandler)
// ❌ 悪い例(SQL Injection脆弱)
const userId = req.params.id
const user = await db.query(`SELECT * FROM users WHERE id = ${userId}`)
// ✅ 良い例(Prisma使用)
const user = await prisma.user.findUnique({
where: { id: userId }
})
// ✅ 良い例(プリペアドステートメント)
const user = await db.query('SELECT * FROM users WHERE id = $1', [userId])
import cors from 'cors'
app.use(cors({
origin: process.env.CLIENT_URL, // 本番環境では特定のドメインのみ
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
}))
import rateLimit from 'express-rate-limit'
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15分
max: 100, // 最大100リクエスト
message: 'Too many requests'
})
app.use('/api/', limiter)
import { z } from 'zod'
// スキーマ定義
const createUserSchema = z.object({
name: z.string().min(1).max(100),
email: z.string().email(),
password: z.string().min(8)
})
// バリデーション
async function createUser(req, res) {
try {
const data = createUserSchema.parse(req.body)
// ユーザー作成処理
const user = await prisma.user.create({
data: {
...data,
password: await hashPassword(data.password)
}
})
res.status(201).json({ data: user })
} catch (error) {
if (error instanceof z.ZodError) {
throw new ValidationError('Invalid input', error.errors)
}
throw error
}
}
// routes/users.ts
import express from 'express'
import { prisma } from '../lib/prisma'
import { authMiddleware } from '../middleware/auth'
import { z } from 'zod'
const router = express.Router()
// GET /api/users
router.get('/', authMiddleware, async (req, res) => {
const users = await prisma.user.findMany({
select: { id: true, name: true, email: true }
})
res.json({ data: users })
})
// GET /api/users/:id
router.get('/:id', authMiddleware, async (req, res) => {
const user = await prisma.user.findUnique({
where: { id: req.params.id },
select: { id: true, name: true, email: true }
})
if (!user) {
throw new NotFoundError('User')
}
res.json({ data: user })
})
// POST /api/users
router.post('/', async (req, res) => {
const schema = z.object({
name: z.string().min(1),
email: z.string().email(),
password: z.string().min(8)
})
const data = schema.parse(req.body)
const user = await prisma.user.create({
data: {
...data,
password: await hashPassword(data.password)
},
select: { id: true, name: true, email: true }
})
res.status(201).json({ data: user })
})
// PUT /api/users/:id
router.put('/:id', authMiddleware, async (req, res) => {
const schema = z.object({
name: z.string().min(1).optional(),
email: z.string().email().optional()
})
const data = schema.parse(req.body)
const user = await prisma.user.update({
where: { id: req.params.id },
data,
select: { id: true, name: true, email: true }
})
res.json({ data: user })
})
// DELETE /api/users/:id
router.delete('/:id', authMiddleware, requireRole(Role.ADMIN), async (req, res) => {
await prisma.user.delete({
where: { id: req.params.id }
})
res.status(204).send()
})
export default router
// routes/auth.ts
router.post('/register', async (req, res) => {
const schema = z.object({
name: z.string().min(1),
email: z.string().email(),
password: z.string().min(8)
})
const data = schema.parse(req.body)
// メール重複チェック
const existing = await prisma.user.findUnique({
where: { email: data.email }
})
if (existing) {
throw new ValidationError('Email already exists')
}
// ユーザー作成
const user = await prisma.user.create({
data: {
...data,
password: await hashPassword(data.password)
}
})
// トークン生成
const token = generateToken(user.id)
res.status(201).json({
data: { user: { id: user.id, name: user.name, email: user.email } },
token
})
})
router.post('/login', async (req, res) => {
const schema = z.object({
email: z.string().email(),
password: z.string()
})
const { email, password } = schema.parse(req.body)
// ユーザー検索
const user = await prisma.user.findUnique({
where: { email }
})
if (!user || !(await comparePassword(password, user.password))) {
throw new UnauthorizedError('Invalid credentials')
}
// トークン生成
const token = generateToken(user.id)
res.json({
data: { user: { id: user.id, name: user.name, email: user.email } },
token
})
})
router.get('/me', authMiddleware, async (req, res) => {
const user = await prisma.user.findUnique({
where: { id: req.userId },
select: { id: true, name: true, email: true }
})
res.json({ data: user })
})
CRUD API作成
/api/posts のCRUD APIを作成してください。
以下を含めてください:
- GET /api/posts(一覧取得)
- GET /api/posts/:id(詳細取得)
- POST /api/posts(作成)
- PUT /api/posts/:id(更新)
- DELETE /api/posts/:id(削除)
- Zodでバリデーション
- 認証ミドルウェア
認証機能実装
JWT認証を実装してください。
以下を含めてください:
- POST /api/auth/register(登録)
- POST /api/auth/login(ログイン)
- GET /api/auth/me(現在のユーザー取得)
- パスワードハッシュ化(bcrypt)
Last updated: 2025-12-24
tools
Fundamentals of modern web development. Framework selection (React, Vue, Next.js), project architecture, state management, routing, build tools, and CSS strategy best practices.
development
# React Development — Complete Guide > A comprehensive guide to building modern React applications with TypeScript. Covers fundamentals through advanced patterns, Hooks mastery, TypeScript integration, performance optimization, and algorithm internals. ## Target Audience - Developers new to React who want a solid foundation - Intermediate React developers looking to deepen their understanding of Hooks and TypeScript patterns - Engineers who want to understand React's internal algorithms (Virt
development
# Node.js Development Skill > A practical guide collection for Node.js development. Covers all aspects of Node.js application development, including Express, NestJS, asynchronous patterns, and performance optimization. ## Overview This skill covers the following topics: - **Express & NestJS**: When to use a lightweight framework vs. an enterprise framework - **Asynchronous Patterns**: Promise, async/await, Event Emitter, Streams, Worker Threads, Cluster - **Performance Optimization**: Memory
development
# Backend Development — Complete Guide > A comprehensive guide to backend engineering. Covers the fundamentals of HTTP, REST API design, databases, authentication, environment configuration, and algorithm proofs — everything needed to build robust server-side systems. ## Target Audience - Developers new to backend engineering - Frontend engineers expanding toward full-stack development - Engineers looking to solidify their understanding of server-side fundamentals ## Prerequisites - Basic p