- server.js: Express 서버, 정적파일 서빙 + API 3개 - GET /api/greeting (랜덤 한국어 인사말) - POST /api/echo (에코 테스트) - GET /api/stats (서버 업타임/버전 정보) - 정적 파일을 public/ 디렉토리로 이동 - index.html에서 fetch로 API 호출하는 버튼 추가 - .gitignore, package.json 추가
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
const express = require('express');
|
|
const path = require('path');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
app.use(express.json());
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
// 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(),
|
|
});
|
|
});
|
|
|
|
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, () => {
|
|
console.log(`서버 실행 중: http://localhost:${PORT}`);
|
|
});
|