/
malklopp
/
opencode-course
Обзор
Документация
Войти
/
malklopp
/
opencode-course
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
practice/src/app.js
69 строк
2 KB
malklop
Курс изучения OpenCode: модули 0-5, учебный Express-проект, README
10 авг 2026, 22:38
10 авг 2026, 22:38
aa4d518
Код
Авторство
О чём код?
const express = require('express') const { listNotes, getNote, createNote, updateNote, deleteNote } = require('./db') const app = express() app.use(express.json({ limit: '100kb' })) app.use(express.urlencoded({ extended: true })) app.get('/health', (req, res) => { res.json({ status: 'ok' }) }) app.get('/notes', (req, res) => { res.json(listNotes()) }) app.get('/notes/:id', (req, res) => { const note = getNote(req.params.id) if (!note) return res.status(404).json({ error: 'Note not found' }) res.json(note) }) app.post('/notes', (req, res) => { const { title, body } = req.body || {} if (!title || typeof title !== 'string' || !title.trim()) { return res.status(400).json({ error: 'title is required' }) } if (body !== undefined && typeof body !== 'string') { return res.status(400).json({ error: 'body must be a string' }) } const note = createNote({ title: title.trim(), body: body ?? '' }) res.status(201).json(note) }) app.put('/notes/:id', (req, res) => { const { title, body } = req.body || {} if (title !== undefined && (typeof title !== 'string' || !title.trim())) { return res.status(400).json({ error: 'title must be a non-empty string' }) } if (body !== undefined && typeof body !== 'string') { return res.status(400).json({ error: 'body must be a string' }) } const note = updateNote(req.params.id, { title: title === undefined ? undefined : title.trim(), body, }) if (!note) return res.status(404).json({ error: 'Note not found' }) res.json(note) }) app.delete('/notes/:id', (req, res) => { const deleted = deleteNote(req.params.id) if (!deleted) return res.status(404).json({ error: 'Note not found' }) res.status(204).end() }) app.use((req, res) => { res.status(404).json({ error: 'Route not found' }) }) app.use((err, req, res, next) => { if (err.type === 'entity.parse.failed' || err.type === 'entity.too.large') { return res.status(400).json({ error: 'Invalid request body' }) } console.error(err) res.status(500).json({ error: 'Internal server error' }) }) module.exports = app