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.

203 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('/admin/network')
  101. .get(adminCtrl.network);
  102. apiRoutes.route('/admin/user/network/:userid')
  103. .get(adminCtrl.getUserNetwork);
  104. apiRoutes.route('/admin/travels/id/:travelid')
  105. .delete(adminCtrl.deleteTravel);
  106. apiRoutes.route('/admin/users/id/:userid')
  107. .delete(adminCtrl.deleteUser);
  108. apiRoutes.route('/admin/users/validate/id/:userid')
  109. .post(adminCtrl.validateUser);
  110. apiRoutes.route('/admin/users/unvalidate/id/:userid')
  111. .post(adminCtrl.unvalidateUser);
  112. apiRoutes.route('/search/:searchstring')
  113. .get(searchCtrl.searchByString);
  114. apiRoutes.route('/numnotifications')
  115. .get(userCtrl.getNumNotificationsByToken);
  116. apiRoutes.route('/notifications')
  117. .get(userCtrl.getNotifications);
  118. apiRoutes.route('/users/token')
  119. .get(userCtrl.getUserByToken);
  120. apiRoutes.route('/users')//agafa l'user a partir del token
  121. .put(userCtrl.updateUser)//no comprovat
  122. .delete(userCtrl.deleteUser);
  123. apiRoutes.route('/changePassword')//agafa l'user a partir del token
  124. .put(userCtrl.changePassword);
  125. apiRoutes.route('/users/id/travels/:userid')
  126. .get(userCtrl.getTravelsByUserId);
  127. apiRoutes.route('/travels')
  128. .post(travelCtrl.addTravel);
  129. apiRoutes.route('/travels/id/modify/:travelid')
  130. .put(travelCtrl.updateTravel)//no comprovat
  131. .delete(travelCtrl.deleteTravel);
  132. apiRoutes.route('/travels/join/:travelid')
  133. .post(travelCtrl.addJoinPetition);
  134. apiRoutes.route('/travels/unjoin/:travelid')
  135. .post(travelCtrl.unJoin);
  136. apiRoutes.route('/travels/leave/:travelid')
  137. .post(travelCtrl.leave);
  138. apiRoutes.route('/travels/declineJoin/:travelid')
  139. .post(travelCtrl.declineJoin);
  140. apiRoutes.route('/travels/acceptJoin/:travelid')
  141. .post(travelCtrl.acceptJoin);
  142. apiRoutes.route('/users/id/likes/:userid')
  143. .get(userCtrl.getUserLikes);
  144. apiRoutes.route('/users/id/like/:userid')
  145. .post(userCtrl.likeUser);
  146. apiRoutes.route('/users/id/unlike/:userid')
  147. .post(userCtrl.unlikeUser);
  148. //FINS AQUÏ COMPROVAT
  149. apiRoutes.route('/travels/comment/:travelid')
  150. .get(travelCtrl.getCommentsByTravelId);
  151. /*apiRoutes.route('/travels/join/:travelId')
  152. .post(travelCtrl.addJoin);
  153. apiRoutes.route('/travels/unjoin/:travelId')
  154. .post(travelCtrl.doUnjoin);*/
  155. /*apiRoutes.route('/travels/:travelId/join')
  156. .post(travelCtrl.addJoin);
  157. apiRoutes.route('/travels/:travelId/unjoin')
  158. .post(travelCtrl.doUnjoin);
  159. */
  160. apiRoutes.route('/users/:userId/fav')
  161. .post(userCtrl.addFav);
  162. apiRoutes.route('/users/:userId/unfav')
  163. .post(userCtrl.doUnfav);
  164. apiRoutes.route('/travels/:travelId/comment')
  165. .post(travelCtrl.addComment);
  166. app.use('/api', apiRoutes);
  167. // end of API routes -------------------------------------
  168. // Start server
  169. app.listen(config.port, function() {
  170. console.log("Node server running on http://localhost:3000");
  171. });