breakhours-transform/server.js
2024-10-03 13:55:18 +02:00

64 lines
1.6 KiB
JavaScript

import express from 'express';
import fetch from 'node-fetch';
import { promises as fs } from 'fs';
import http from 'http';
import https from 'https';
import cors from 'cors';
import open from 'open';
import path from 'path';
import { fileURLToPath } from 'url';
import router from './router.js'
import { login } from './api/auth.js'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = 22000;
app.use(cors())
app.use(express.json({ limit: '50mb' }));
app.use(express.static('data'));
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/api/time-table', (req, res) => {
res.sendFile(path.join(__dirname, 'data', 'colorcrew_data_transformed_prod.json'));
});
///////////
// Routes
// auth
app.post('/api/login', async (req, res) => {
const { userName, passWord } = req.body
const token = await login(userName, passWord)
if (token) {
res.status(200).send({ token })
} else {
res.status(403).send({ error: 'token not found' })
}
})
app.use('/api', router);
async function startServer() {
try {
const options = {
key: await fs.readFile('./certs/server.key'),
cert: await fs.readFile('./certs/server.cert')
};
https.createServer(options, app).listen(port, () => {
console.log(`HTTPS Server running at https://localhost:${port}`);
// open(`https://localhost:${port}`);
});
} catch (err) {
console.error('Error starting HTTPS server:', err);
}
}
startServer();