Skip to content

Commit a769f3e

Browse files
committed
added port true in viteconfig, and updated server file
1 parent bd46298 commit a769f3e

File tree

5 files changed

+88
-83
lines changed

5 files changed

+88
-83
lines changed

backend/serve.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

backend/server.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
11
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';
73
import externalIdGenerator from './externalIDGenerator.js';
4+
import MixedMetrix from './cloudwatch.js';
5+
import authenticateToken from './controllers/authMiddleware.js';
86

9-
const port = 3000;
10-
const app = express();
7+
const port = 81;
118

12-
const __dirname =
13-
path.dirname(fileURLToPath(import.meta.url)) || path.resolve();
9+
const app = express();
1410

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+
);
1520
app.use(express.json());
1621
app.use(express.urlencoded({ extended: true }));
17-
app.use(express.static(path.join(__dirname, 'dist')));
1822

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 });
2526
});
2627

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 });
3132
});
3233

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 });
3638
});
3739

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'));
4146

42-
//default global error handler
4347
app.use((err, req, res, next) => {
4448
const defaultErr = {
45-
log: 'Express error handler caught unknown middleware error',
49+
log: 'Something Went Wrong in server 2',
4650
status: 500,
4751
message: { err: 'An error occurred' },
4852
};

backend/serverDelete.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//CAN BE DELETED
2+
// import express from 'express';
3+
// import { fileURLToPath } from 'node:url';
4+
// import path from 'node:path';
5+
// import { awsData, awsHourData } from './data.js';
6+
// import Awsrouter from './routes/ApiRoutes.js';
7+
// import externalIdGenerator from './externalIDGenerator.js';
8+
9+
// const port = 3000;
10+
// const app = express();
11+
12+
// const __dirname =
13+
// path.dirname(fileURLToPath(import.meta.url)) || path.resolve();
14+
15+
// app.use(express.json());
16+
// app.use(express.urlencoded({ extended: true }));
17+
// app.use(express.static(path.join(__dirname, 'dist')));
18+
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 });
25+
// });
26+
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);
31+
// });
32+
33+
34+
35+
// app.use((req, res) =>
36+
// res.status(404).send("This is not the page you're looking for...")
37+
// );
38+
39+
// //default global error handler
40+
// app.use((err, req, res, next) => {
41+
// const defaultErr = {
42+
// log: 'Express error handler caught unknown middleware error',
43+
// status: 500,
44+
// message: { err: 'An error occurred' },
45+
// };
46+
// const errorObj = Object.assign({}, defaultErr, err);
47+
// console.log(errorObj.log);
48+
// return res.status(errorObj.status).json(errorObj.message);
49+
// });
50+
51+
// app.listen(port, () => {
52+
// console.log(`Server started at http://localhost:${port}`);
53+
// });

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "0.0.0",
55
"type": "module",
66
"scripts": {
7-
"dev": "vite & nodemon ./backend/serve.js",
7+
"dev": "vite & nodemon ./backend/server.js",
88
"build": "vite build",
99
"lint": "eslint .",
1010
"preview": "vite preview & node ./backend/server.js",

vite.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export default defineConfig({
1919
},
2020
},
2121
server: {
22-
host: '0.0.0.0',
22+
port: 3000, //runs frontend on 3000
23+
open: true, //will automatically open up the page
2324
proxy: {
2425
//can be deleted
2526
// '/login': 'http://localhost:3000',

0 commit comments

Comments
 (0)