You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
6.4 KiB

7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. var express = require("express"),
  2. app = express(),
  3. bodyParser = require("body-parser"),
  4. methodOverride = require("method-override"),
  5. mongoose = require('mongoose');
  6. var morgan = require('morgan');
  7. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  8. var config = require('./config'); // get our config file
  9. mongoose.Promise = global.Promise;
  10. // Connection to DB
  11. mongoose.connect(config.database, function(err, res) {
  12. if (err) throw err;
  13. console.log('Connected to Database');
  14. });
  15. app.set('superSecret', config.secret); // secret variable
  16. // Middlewares
  17. /*app.use(bodyParser.urlencoded({
  18. extended: false
  19. }));
  20. app.use(bodyParser.json());*/
  21. app.use(bodyParser.json({limit: '50mb'}));
  22. app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
  23. app.use(methodOverride());
  24. // use morgan to log requests to the console
  25. app.use(morgan('dev'));
  26. // Import Models and controllers
  27. var userMdl = require('./models/userModel')(app, mongoose);
  28. var notificationMdl = require('./models/notificationModel')(app, mongoose);
  29. var travelMdl = require('./models/travelModel')(app, mongoose);
  30. var commentMdl = require('./models/commentModel')(app, mongoose);
  31. var adminMdl = require('./models/adminModel')(app, mongoose);
  32. var userCtrl = require('./controllers/userController');
  33. var searchCtrl = require('./controllers/searchController');
  34. var travelCtrl = require('./controllers/travelController');
  35. var adminCtrl = require('./controllers/adminController');
  36. /*// Example Route
  37. var router = express.Router();
  38. router.get('/', function(req, res) {
  39. res.send("Hello world!");
  40. });
  41. app.use(router);*/
  42. app.use(express.static(__dirname + '/www'));
  43. //CORS
  44. app.use(function(req, res, next) {
  45. res.header("Access-Control-Allow-Origin", "*");
  46. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  47. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token");
  48. next();
  49. });
  50. // API routes ------------------------------------------------------
  51. var apiRoutes = express.Router();
  52. apiRoutes.route('/login')
  53. .post(userCtrl.login);
  54. apiRoutes.route('/signup')
  55. .post(userCtrl.signup);
  56. apiRoutes.route('/users')
  57. .get(userCtrl.getAllUsers);
  58. apiRoutes.route('/users/id/:userid')
  59. .get(userCtrl.getUserById);
  60. apiRoutes.route('/travels')
  61. .get(travelCtrl.getAllTravels);
  62. apiRoutes.route('/travels/id/:travelid')
  63. .get(travelCtrl.getTravelById);
  64. apiRoutes.route('/admin/login')
  65. .post(adminCtrl.login);
  66. apiRoutes.route('/admin/signup')
  67. .post(adminCtrl.signup);
  68. // OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
  69. // route middleware to verify a token
  70. apiRoutes.use(function(req, res, next) {
  71. // check header or url parameters or post parameters for token
  72. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  73. // decode token
  74. if (token) {
  75. // verifies secret and checks exp
  76. jwt.verify(token, app.get('superSecret'), function(err, decoded) {
  77. if (err) {
  78. return res.send(204,
  79. {
  80. success: false,
  81. message: 'Failed to authenticate token.'
  82. });
  83. } else {
  84. // if everything is good, save to request for use in other routes
  85. req.decoded = decoded;
  86. //console.log("decoded " + decoded);
  87. next();
  88. }
  89. });
  90. } else {
  91. // if there is no token
  92. // return an error
  93. return res.status(204).send({
  94. success: false,
  95. message: 'No token provided.'
  96. });
  97. }
  98. }); //fi verificació de token
  99. //admin
  100. apiRoutes.route('/admins')
  101. .get(adminCtrl.getAllAdmins);
  102. apiRoutes.route('/admin/network')
  103. .get(adminCtrl.network);
  104. apiRoutes.route('/admin/user/network/:userid')
  105. .get(adminCtrl.getUserNetwork);
  106. apiRoutes.route('/admin/travels/id/:travelid')
  107. .delete(adminCtrl.deleteTravel);
  108. apiRoutes.route('/admin/users/id/:userid')
  109. .delete(adminCtrl.deleteUser);
  110. apiRoutes.route('/admin/users/validate/id/:userid')
  111. .post(adminCtrl.validateUser);
  112. apiRoutes.route('/admin/users/unvalidate/id/:userid')
  113. .post(adminCtrl.unvalidateUser);
  114. apiRoutes.route('/search/:searchstring')
  115. .get(searchCtrl.searchByString);
  116. apiRoutes.route('/numnotifications')
  117. .get(userCtrl.getNumNotificationsByToken);
  118. apiRoutes.route('/notifications')
  119. .get(userCtrl.getNotifications);
  120. apiRoutes.route('/users/token')
  121. .get(userCtrl.getUserByToken);
  122. apiRoutes.route('/users')//agafa l'user a partir del token
  123. .put(userCtrl.updateUser)//no comprovat
  124. .delete(userCtrl.deleteUser);
  125. apiRoutes.route('/changePassword')//agafa l'user a partir del token
  126. .put(userCtrl.changePassword);
  127. apiRoutes.route('/users/id/travels/:userid')
  128. .get(userCtrl.getTravelsByUserId);
  129. apiRoutes.route('/travels')
  130. .post(travelCtrl.addTravel);
  131. apiRoutes.route('/travels/id/modify/:travelid')
  132. .put(travelCtrl.updateTravel)//no comprovat
  133. .delete(travelCtrl.deleteTravel);
  134. apiRoutes.route('/travels/join/:travelid')
  135. .post(travelCtrl.addJoinPetition);
  136. apiRoutes.route('/travels/unjoin/:travelid')
  137. .post(travelCtrl.unJoin);
  138. apiRoutes.route('/travels/leave/:travelid')
  139. .post(travelCtrl.leave);
  140. apiRoutes.route('/travels/declineJoin/:travelid')
  141. .post(travelCtrl.declineJoin);
  142. apiRoutes.route('/travels/acceptJoin/:travelid')
  143. .post(travelCtrl.acceptJoin);
  144. apiRoutes.route('/users/id/likes/:userid')
  145. .get(userCtrl.getUserLikes);
  146. apiRoutes.route('/users/id/like/:userid')
  147. .post(userCtrl.likeUser);
  148. apiRoutes.route('/users/id/unlike/:userid')
  149. .post(userCtrl.unlikeUser);
  150. //FINS AQUÏ COMPROVAT
  151. apiRoutes.route('/travels/comment/:travelid')
  152. .get(travelCtrl.getCommentsByTravelId);
  153. /*apiRoutes.route('/travels/join/:travelId')
  154. .post(travelCtrl.addJoin);
  155. apiRoutes.route('/travels/unjoin/:travelId')
  156. .post(travelCtrl.doUnjoin);*/
  157. /*apiRoutes.route('/travels/:travelId/join')
  158. .post(travelCtrl.addJoin);
  159. apiRoutes.route('/travels/:travelId/unjoin')
  160. .post(travelCtrl.doUnjoin);
  161. */
  162. apiRoutes.route('/users/:userId/fav')
  163. .post(userCtrl.addFav);
  164. apiRoutes.route('/users/:userId/unfav')
  165. .post(userCtrl.doUnfav);
  166. apiRoutes.route('/travels/:travelId/comment')
  167. .post(travelCtrl.addComment);
  168. app.use('/api', apiRoutes);
  169. // end of API routes -------------------------------------
  170. // Start server
  171. app.listen(config.port, function() {
  172. console.log("Node server running on http://localhost:3000");
  173. });