-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
50 lines (46 loc) · 1.22 KB
/
router.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
const TextController = require("./controllers/textController")
const databaseManager = require("./controllers/databaseManager")
const userController = require("./controllers/userController")
const filesManager = require("./controllers/filesManager")
let textController = new TextController();
const routes = {
'/something2' : {
'GET': textController.showHelloWorld2
},
'/something' : {
'GET': textController.showHelloWorld
},
'/initDB': {
'POST': databaseManager.initDB
},
'/login': {
'POST': userController.login
},
'/logout': {
'POST': userController.logout
},
'/register': {
'POST': userController.register
},
'/file': {
'POST': filesManager.uploadFile,
'GET': filesManager.downloadFile
},
'/files': {
'GET': filesManager.listFiles
}
}
class Router {
navigate (req, res, next) {
let url = req.url.split('?')[0];
let functionToCall = routes && routes[url] && routes[url][req.method];
//console.log(req.url, req.method, routes[req.url], functionToCall);
if(functionToCall) {
functionToCall(req, res, next);
} else {
console.log("route not found:", url, req.method)
next(new Error("Route not found"));
}
}
}
module.exports = Router