-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (49 loc) · 1.42 KB
/
index.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
require("dotenv").config();
const NodeRSA = require('node-rsa');
const express = require("express");
const app = express();
const cors = require("cors");
const bodyParser = require("body-parser");
const errorHandler = require("./controllers/error");
const db = require("./models");
const { loginRequired } = require("./middleware/auth");
const userRoutes = require("./routes/users");
const eventsRoutes = require("./routes/events");
const achievementsRoutes = require("./routes/achievements");
const PORT = process.env.PORT || 3000;
init();
async function init(){
try{
await generateServerKey();
}
catch(err){
console.log(`${err}\n\nShutting down...`);
return;
}
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use("/api/users", userRoutes);
// app.use("/api/achievements", loginRequired, achievementsRoutes);
app.use("/api/achievements", achievementsRoutes);
app.use("/api/events", eventsRoutes);
app.use(function (req, res, next) {
let err = new Error("Not Found");
err.status = 404;
next(err);
});
app.use(errorHandler);
app.listen(PORT, function () {
console.log(`Server is starting on port ${PORT}`);
});
}
async function generateServerKey(){
let key = await db.Keys.findOne();
if(key !== null)
return;
key = new NodeRSA({b: 512});
await db.Keys.create( {
public: key.exportKey('public'),
private: key.exportKey('private')
})
}