54 أسطر
1.3 KiB
JavaScript
54 أسطر
1.3 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const bodyParser = require('body-parser');
|
|
|
|
const app = express();
|
|
const PORT = 3000;
|
|
|
|
let requestCount = 0;
|
|
|
|
// Middleware
|
|
app.use(bodyParser.json());
|
|
app.use((req, res, next) => {
|
|
requestCount++;
|
|
next();
|
|
});
|
|
|
|
// Serve static frontend
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
|
|
|
// Serve input JSON files
|
|
app.use('/input', express.static(path.join(__dirname, 'input')));
|
|
|
|
// Serve output folder (for history.json)
|
|
app.use('/output', express.static(path.join(__dirname, 'output')));
|
|
|
|
// API to save history data
|
|
app.post('/save-history', (req, res) => {
|
|
const historyPath = path.join(__dirname, 'output', 'history.json');
|
|
const json = JSON.stringify(req.body, null, 2);
|
|
|
|
fs.writeFile(historyPath, json, 'utf8', (err) => {
|
|
if (err) {
|
|
console.error('Error saving history.json:', err);
|
|
res.status(500).send('Failed to save history.json');
|
|
} else {
|
|
console.log('History successfully saved.');
|
|
res.status(200).send('Saved');
|
|
}
|
|
});
|
|
});
|
|
|
|
// Health Check Endpoint
|
|
app.get('/health', (req, res) => {
|
|
res.status(200).json({
|
|
status: 'UP',
|
|
requestCount: requestCount
|
|
});
|
|
});
|
|
|
|
// Start server
|
|
app.listen(PORT, () => {
|
|
console.log(`Server running at http://localhost:${PORT}`);
|
|
}); |