-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (64 loc) · 2.04 KB
/
server.js
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
"use strict";
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const helmet = require("helmet");
const connectDB = require("./config/db.js");
const apiRoutes = require("./routes/api.js");
const fccTestingRoutes = require("./routes/fcctesting.js");
const runner = require("./test-runner");
const app = express();
app.use(
helmet({
xFrameOptions: { action: "sameorigin" },
xDnsPrefetchControl: { allow: false },
referrerPolicy: { policy: "same-origin" },
})
);
app.use("/public", express.static(process.cwd() + "/public"));
app.use(cors({ origin: "*" })); //For FCC testing purposes only
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
async function start() {
try {
await connectDB.connect();
//Sample front-end
app.route("/b/:board/").get(function (req, res) {
res.sendFile(process.cwd() + "/views/board.html");
});
app.route("/b/:board/:threadid").get(function (req, res) {
res.sendFile(process.cwd() + "/views/thread.html");
});
//Index page (static HTML)
app.route("/").get(function (req, res) {
res.sendFile(process.cwd() + "/views/index.html");
});
//For FCC testing purposes
fccTestingRoutes(app);
//Routing for API
apiRoutes(app);
//404 Not Found Middleware
app.use(function (req, res, next) {
res.status(404).type("text").send("Not Found");
});
//Start our server and tests!
const listener = app.listen(process.env.PORT || 3000, function () {
console.log("Your app is listening on port " + listener.address().port);
if (process.env.NODE_ENV === "test") {
console.log("Running Tests...");
setTimeout(function () {
try {
runner.run();
} catch (e) {
console.log("Tests are not valid:");
console.error(e);
}
}, 1500);
}
});
} catch (error) {
console.log("Error: error al conectar a la base de datos" + error.message);
}
}
start();
module.exports = app; //for testing