forked from Matterwiki/Matterwiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·177 lines (138 loc) · 4.3 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
This is main file which will contain all of our endpoints.
Once we have enough endpoints defined we start breaking them into modules for better code readability
*/
// Importing all the required libraries
var express = require('express');
var bodyParser = require('body-parser'); //body parser to parse the request body
var db = require('./db.js'); //this file contains the knex file import. it's equal to knex=require('knex')
var app = express();
var fs = require('fs');
var apiRoutes = express.Router();
var apiRoutesAdmin = express.Router();
var jwt = require('jsonwebtoken');
var misc = require('./misc.js');
var config = require('./config'); //config file in the app directory which contains the JWT key
process.env.PORT = process.env.PORT || 5000;
console.log(process.env.NODE_ENV);
if(process.env.NODE_ENV !== 'production') {
// add some patchwork for the devserver to work!
require('./webpack-middleware')(app);
}
app.set('superSecret', config.auth_secret); // secret variable
// Using the body parser middleware to parse request body
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.get('/api',function(req,res){
// this is just a sample endpoint I build to see if things are working
res.send("Hey! You're looking at the matterwiki API");
});
// Importing all endpoints for authentication
require('./api/authentication')(app);
// Importing the setup endpoint
require('./api/setup')(app);
apiRoutes.use(function(req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks for expiration
jwt.verify(token, app.get('superSecret'), function(err, decoded) {
if (err) {
return res.json({
error: {
error: true,
message: 'Failed to authenticate token'
},
code: 'B101',
data: {
}
});
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
return res.status(403).json({
error: {
error: true,
message: 'No token provided'
},
code: 'B102',
data: {
}
});
}
});
apiRoutesAdmin.use(function(req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks for expiration
jwt.verify(token, app.get('superSecret'), function(err, decoded) {
if (err) {
return res.json({
error: {
error: true,
message: 'Failed to authenticate token'
},
code: 'B101',
data: {
}
});
} else {
if(decoded.id == 1) {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
else {
return res.status(403).json({
error: {
error: true,
message: 'You are not authorized to perform this action'
},
code: 'BNOTADMIN',
data: {
}
});
}
}
});
} else {
// if there is no token
// return an error
return res.status(403).json({
error: {
error: true,
message: 'No token provided'
},
code: 'B102',
data: {
}
});
}
});
// Importing all endpoints for articles
require('./api/articles')(apiRoutes);
// Importing all endpoints for topics
require('./api/topics')(apiRoutes);
// Importing all endpoints for users
require('./api/users')(apiRoutesAdmin);
// Importing all endpoints for archives
require('./api/archives')(apiRoutes);
// Importing the search endpoint
require('./api/search')(apiRoutes);
// Importing all endpoints which are only admin accessible
require('./api/admin')(apiRoutesAdmin);
app.use('/api', apiRoutes);
app.use('/api', apiRoutesAdmin);
app.use(express.static(__dirname + '/client'));
app.listen(process.env.PORT, function(){
console.log("The magic is happening on port %s", process.env.PORT);
});