-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (44 loc) · 1.47 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
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const path = require("path");
const portNumber = 4000;
// Make mongoose connection
mongoose.connect(`mongodb://localhost:27017/reactive-hitomi-server`);
let db = mongoose.connection;
// Set events for mongoose.
db.once("open", function() {
console.log("Connected to MongoDB");
});
db.on("error", function(err) {
console.log(`MongoDB error: ${JSON.stringify(err)}`);
});
// Setup express
const app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'client/build')));
// Express routing
// API
let cacheRoute = require("./src/routes/api/cache");
let galleryRoute = require("./src/routes/api/gallery");
let originalRoute = require("./src/routes/api/original");
let thumbsRoute = require("./src/routes/api/thumbs");
let pageRoute = require("./src/routes/api/page");
let videoRoute = require("./src/routes/api/video");
app.use("/api/cache", cacheRoute);
app.use("/api/gallery", galleryRoute);
app.use("/api/original", originalRoute);
app.use("/api/thumbs", thumbsRoute);
app.use("/api/page", pageRoute);
app.use("/api/video", videoRoute);
app.use("/api/*", (req, res) => {
res.send(`Cannot ${req.method} ${req.path}`);
});
// View
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
// Start listen
app.listen(portNumber, () => {
console.log(`Server started on port ${portNumber}`);
});