|
1 | 1 | import express from 'express';
|
2 |
| -import { fileURLToPath } from 'node:url'; |
3 |
| -import path from 'node:path'; |
4 |
| -import { awsData, awsHourData } from './data.js'; |
5 |
| -import authenticateToken from './controllers/authMiddleware.js'; |
6 |
| -import Awsrouter from './routes/ApiRoutes.js'; |
| 2 | +import cors from 'cors'; |
7 | 3 | import externalIdGenerator from './externalIDGenerator.js';
|
| 4 | +import MixedMetrix from './cloudwatch.js'; |
| 5 | +import authenticateToken from './controllers/authMiddleware.js'; |
8 | 6 |
|
9 |
| -const port = 3000; |
10 |
| -const app = express(); |
| 7 | +const port = 81; |
11 | 8 |
|
12 |
| -const __dirname = |
13 |
| - path.dirname(fileURLToPath(import.meta.url)) || path.resolve(); |
| 9 | +const app = express(); |
14 | 10 |
|
| 11 | +app.use( |
| 12 | + cors({ |
| 13 | + origin: true, |
| 14 | + methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], |
| 15 | + allowedHeaders: ['Content-Type'], |
| 16 | + credentials: true, |
| 17 | + preflightContinue: false, |
| 18 | + }) |
| 19 | +); |
15 | 20 | app.use(express.json());
|
16 | 21 | app.use(express.urlencoded({ extended: true }));
|
17 |
| -app.use(express.static(path.join(__dirname, 'dist'))); |
18 | 22 |
|
19 |
| -//CAN BE DELETED, WAS ONCE A TEST ROUTER |
20 |
| -app.use('/aws_services', Awsrouter); |
21 |
| - |
22 |
| -//TEST ROUTE, can be deleted |
23 |
| -app.get('/protected', authenticateToken, (req, res) => { |
24 |
| - res.json({ message: 'You have accessed a protected route!', user: req.user }); |
| 23 | +app.get('/random', async (req, res) => { |
| 24 | + let id = await externalIdGenerator(); |
| 25 | + return res.status(200).json({ id }); |
25 | 26 | });
|
26 | 27 |
|
27 |
| -//VITE CONFIG file allows for this to be just /data instead of /Home/data |
28 |
| -app.get('/data', async (req, res) => { |
29 |
| - let data = await awsHourData(); |
30 |
| - res.status(200).json(data); |
| 28 | +app.post('/data', async (req, res) => { |
| 29 | + const { graph, metric, data } = req.body; |
| 30 | + let result = await MixedMetrix({ graph, metric, data }); |
| 31 | + return res.status(200).json({ result }); |
31 | 32 | });
|
32 | 33 |
|
33 |
| - |
34 |
| -app.get('/protected', authenticateToken, (req, res) => { |
35 |
| - res.status(200).json('Success, accessed a protected route'); |
| 34 | +app.options('/data', async (req, res) => { |
| 35 | + const { graph, metric, data } = req.body; |
| 36 | + let result = await MixedMetrix({ graph, metric, data }); |
| 37 | + return res.status(200).json({ result }); |
36 | 38 | });
|
37 | 39 |
|
38 |
| -app.use((req, res) => |
39 |
| - res.status(404).send("This is not the page you're looking for...") |
40 |
| -); |
| 40 | +// WILL BE USED TO PROTECT ANY REQUESTS FOR DATA (ENSURES USER IS AUTHENTICATED) |
| 41 | +// app.get('/protected', authenticateToken, (req, res) => { |
| 42 | +// res.status(200).json('Success, accessed a protected route'); |
| 43 | +// }); |
| 44 | + |
| 45 | +app.use((req, res) => res.status(404).send('No Data')); |
41 | 46 |
|
42 |
| -//default global error handler |
43 | 47 | app.use((err, req, res, next) => {
|
44 | 48 | const defaultErr = {
|
45 |
| - log: 'Express error handler caught unknown middleware error', |
| 49 | + log: 'Something Went Wrong in server 2', |
46 | 50 | status: 500,
|
47 | 51 | message: { err: 'An error occurred' },
|
48 | 52 | };
|
|
0 commit comments