forked from omarashraf/The-Trivia-Competition-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
57 lines (47 loc) · 1.41 KB
/
express.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
var express = require("express");
var mongoose = require("mongoose");
var mongoDB = "mongodb://localhost:27017/riseup_db";
var app = express();
var cors = require('cors');
var bodyParser = require("body-parser");
var User = require("./models/user");
var userCtrl = require("./controllers/user_ctrl");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
// endpoint for testing
app.get("/", userCtrl.testEndpoint);
/*
- get the ranking of a certain number of players
- need to have the number of top players (in the body of the request) required to be returned
*/
app.post("/leaderboard", userCtrl.getLeaderboard);
/*
- get a specific user
- need to have the username in the body of the request
*/
app.post('/user', userCtrl.getUser);
/*
- update score of specific user
- need to have the score in the body of the request
*/
app.put("/score", userCtrl.updateScore);
/*
- add a new user in the db
- need to have the username in the body of the request
- score is set to 0 initially
*/
app.post("/register", userCtrl.registerUser);
// app is running on port 3000
// TODO: set port among env variables
app.listen(3000, function() {
console.log("Listening on port 3000..");
});
// db connection through mongoose and printing status in console
mongoose.connect(mongoDB, function(err, res) {
if (err) {
console.log("Error --> "+ err);
}
else {
console.log("Succeeded DB connection");
}
});