Files
ipwhitelist-app/server.js
T

41 lines
1.1 KiB
JavaScript

const express = require('express');
const path = require('path');
require('dotenv').config();
const { checkConnection } = require('./src/db');
const app = express();
const PORT = process.env.PORT || 3000;
// Шаблоны
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Статика
app.use(express.static(path.join(__dirname, 'public')));
// Парсинг форм
app.use(express.urlencoded({ extended: true }));
// Health check (для nubes_nodejs)
app.get('/healthz', async (req, res) => {
try {
await checkConnection();
res.status(200).send('OK');
} catch (e) {
res.status(500).send('DB error');
}
});
// Главная страница
app.get('/', (req, res) => {
res.render('index', { title: 'IP WhiteList' });
});
// Не падаем если БД недоступна — healthcheck покажет статус
let dbOk = false;
checkConnection()
.then(() => { dbOk = true; console.log('DB connected'); })
.catch((e) => console.error('DB not ready:', e.message));
app.listen(PORT, () => console.log(`Server on port ${PORT}`));