refactor: Express 제거, 순수 Node.js http 모듈로 교체

- server.js: http 모듈만 사용, 정적 파일 서빙 + API 3개
- package.json: dependencies 제거 (외부 패키지 없음)
- package-lock.json 삭제
This commit is contained in:
2026-06-12 07:24:10 +00:00
parent 1dcca058df
commit 0c55bd5da6
3 changed files with 76 additions and 865 deletions

111
server.js
View File

@@ -1,45 +1,86 @@
const express = require('express');
const http = require('http');
const fs = require('fs');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
const PUBLIC_DIR = path.join(__dirname, 'public');
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
const MIME_TYPES = {
'.html': 'text/html; charset=utf-8',
'.css': 'text/css',
'.js': 'text/javascript',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.ico': 'image/x-icon',
};
// API routes
app.get('/api/greeting', (req, res) => {
const greetings = [
'안녕하세요! 👋',
'반갑습니다! 😊',
'어서오세요! 🌸',
'좋은 하루예요! ☀️',
'환영합니다! 🎉',
];
const message = greetings[Math.floor(Math.random() * greetings.length)];
res.json({ message });
});
app.get('/api/stats', (req, res) => {
res.json({
uptime: Math.floor(process.uptime()),
nodeVersion: process.version,
platform: process.platform,
deployedAt: new Date().toISOString(),
function serveStatic(res, filePath) {
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404);
res.end('Not Found');
return;
}
const ext = path.extname(filePath);
res.writeHead(200, { 'Content-Type': MIME_TYPES[ext] || 'application/octet-stream' });
res.end(data);
});
}
function sendJson(res, status, body) {
res.writeHead(status, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(body));
}
function readBody(req) {
return new Promise((resolve) => {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
try { resolve(JSON.parse(body)); }
catch { resolve({}); }
});
});
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, `http://localhost`);
const pathname = url.pathname;
// API routes
if (pathname === '/api/greeting' && req.method === 'GET') {
const greetings = ['안녕하세요! 👋', '반갑습니다! 😊', '어서오세요! 🌸', '좋은 하루예요! ☀️', '환영합니다! 🎉'];
sendJson(res, 200, { message: greetings[Math.floor(Math.random() * greetings.length)] });
return;
}
if (pathname === '/api/stats' && req.method === 'GET') {
sendJson(res, 200, {
uptime: Math.floor(process.uptime()),
nodeVersion: process.version,
platform: process.platform,
deployedAt: new Date().toISOString(),
});
return;
}
if (pathname === '/api/echo' && req.method === 'POST') {
const { text } = await readBody(req);
if (!text) { sendJson(res, 400, { error: '텍스트를 입력해주세요.' }); return; }
sendJson(res, 200, { echo: text, length: text.length });
return;
}
// Static files
const filePath = path.join(PUBLIC_DIR, pathname === '/' ? 'index.html' : pathname);
const ext = path.extname(filePath);
if (ext) {
serveStatic(res, filePath);
} else {
serveStatic(res, path.join(PUBLIC_DIR, 'index.html'));
}
});
app.post('/api/echo', (req, res) => {
const { text } = req.body;
if (!text) return res.status(400).json({ error: '텍스트를 입력해주세요.' });
res.json({ echo: text, length: text.length });
});
// Fallback to index.html for unknown routes
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
server.listen(PORT, () => {
console.log(`서버 실행 중: http://localhost:${PORT}`);
});