-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
92 lines (83 loc) · 2.97 KB
/
app.js
File metadata and controls
92 lines (83 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import config from 'config';
import fs from 'fs';
import path from 'path';
import express from 'express';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import ReportController from './controllers/ReportController.js';
import InspectController from './controllers/InspectController.js';
import json2obj from './helpers/json2obj.js';
import { parse as json2csv } from 'json2csv';
const app = express();
// basic express server setup
app.set('views', path.resolve(__dirname, 'views'));
app.set('view engine', 'ejs');
// Middleware to serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', async (req, res) => {
const filePath = new URL('./output/summary.json', import.meta.url);
const summary = await json2obj(filePath);
res.render('index', {
title: 'LibGuides Search Results',
summary: summary,
message: 'Check the console for detailed output.',
});
});
app.get('/inspect', (req, res) => {
res.redirect('/'); // Redirect to the main page if no ID is provided
});
app.get('/inspect/:id', async (req, res) => {
const id = req.params.id;
let kwicChars = req.query.kwicChars || undefined;
try {
const results = InspectController(id, kwicChars);
// create url query string for the view
const queryString = new URLSearchParams(req.query).toString();
const types = new URLSearchParams(req.query).get('types');
// console.log(`Query String: ${queryString}`);
if (JSON.stringify(req.query).includes('json')) {
return res.send(results); // JSON response for API requests
}
results.queryString = queryString;
results.types = types; // Add query string to results for the view
res.render('inspect', results); // HTML display
} catch (error) {
console.error(`Error inspecting ID ${id}:`, error);
return res
.status(400)
.send(`Entry not found or invalid ID: ${id}; ${error}`);
}
});
app.get('/report', (req, res) => {
// if file exists in public/docs, deliver it
const filePath = path.join(
__dirname,
'public',
'docs',
'libGuidesSearchReport.csv'
);
if (fs.existsSync(filePath)) {
return res.download(filePath); // Download the report file
}
// If the file does not exist, generate the report
else {
let report = ReportController();
if (JSON.stringify(req.query).includes('json')) {
return res.send(report); // JSON response for API requests
}
// default to CSV output
const csv = json2csv(report.items);
res.header('Content-Type', 'text/csv');
res.setHeader(
'Content-Disposition',
'attachment; filename=libGuidesSearchReport.csv'
);
res.send(csv); // Send CSV file as response
}
});
const PORT = config.get('app.port') || 3000; // Use environment variable or default to 3000
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Go to http://localhost:${PORT}/`);
});