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.

183 lines
5.7 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 userCtrl = require('./controllers/userController');
  32. var searchCtrl = require('./controllers/searchController');
  33. var travelCtrl = require('./controllers/travelController');
  34. /*// Example Route
  35. var router = express.Router();
  36. router.get('/', function(req, res) {
  37. res.send("Hello world!");
  38. });
  39. app.use(router);*/
  40. app.use(express.static(__dirname + '/www'));
  41. //CORS
  42. app.use(function(req, res, next) {
  43. res.header("Access-Control-Allow-Origin", "*");
  44. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  45. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token");
  46. next();
  47. });
  48. // API routes ------------------------------------------------------
  49. var apiRoutes = express.Router();
  50. apiRoutes.route('/login')
  51. .post(userCtrl.login);
  52. apiRoutes.route('/signup')
  53. .post(userCtrl.signup);
  54. apiRoutes.route('/users')
  55. .get(userCtrl.getAllUsers);
  56. apiRoutes.route('/users/id/:userid')
  57. .get(userCtrl.getUserById);
  58. apiRoutes.route('/travels')
  59. .get(travelCtrl.getAllTravels);
  60. apiRoutes.route('/travels/id/:travelid')
  61. .get(travelCtrl.getTravelById);
  62. // OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
  63. // route middleware to verify a token
  64. apiRoutes.use(function(req, res, next) {
  65. // check header or url parameters or post parameters for token
  66. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  67. // decode token
  68. if (token) {
  69. // verifies secret and checks exp
  70. jwt.verify(token, app.get('superSecret'), function(err, decoded) {
  71. if (err) {
  72. return res.send(204,
  73. {
  74. success: false,
  75. message: 'Failed to authenticate token.'
  76. });
  77. } else {
  78. // if everything is good, save to request for use in other routes
  79. req.decoded = decoded;
  80. //console.log("decoded " + decoded);
  81. next();
  82. }
  83. });
  84. } else {
  85. // if there is no token
  86. // return an error
  87. return res.status(204).send({
  88. success: false,
  89. message: 'No token provided.'
  90. });
  91. }
  92. }); //fi verificació de token
  93. apiRoutes.route('/search/:searchstring')
  94. .get(searchCtrl.searchByString);
  95. apiRoutes.route('/numnotifications')
  96. .get(userCtrl.getNumNotificationsByToken);
  97. apiRoutes.route('/notifications')
  98. .get(userCtrl.getNotifications);
  99. apiRoutes.route('/users/token')
  100. .get(userCtrl.getUserByToken);
  101. apiRoutes.route('/users')//agafa l'user a partir del token
  102. .put(userCtrl.updateUser)//no comprovat
  103. .delete(userCtrl.deleteUser);
  104. apiRoutes.route('/changePassword')//agafa l'user a partir del token
  105. .put(userCtrl.changePassword);
  106. apiRoutes.route('/users/id/travels/:userid')
  107. .get(userCtrl.getTravelsByUserId);
  108. apiRoutes.route('/travels')
  109. .post(travelCtrl.addTravel);
  110. apiRoutes.route('/travels/id/modify/:travelid')
  111. .put(travelCtrl.updateTravel)//no comprovat
  112. .delete(travelCtrl.deleteTravel);
  113. apiRoutes.route('/travels/join/:travelid')
  114. .post(travelCtrl.addJoinPetition);
  115. apiRoutes.route('/travels/unjoin/:travelid')
  116. .post(travelCtrl.unJoin);
  117. apiRoutes.route('/travels/leave/:travelid')
  118. .post(travelCtrl.leave);
  119. apiRoutes.route('/travels/declineJoin/:travelid')
  120. .post(travelCtrl.declineJoin);
  121. apiRoutes.route('/travels/acceptJoin/:travelid')
  122. .post(travelCtrl.acceptJoin);
  123. apiRoutes.route('/users/id/likes/:userid')
  124. .get(userCtrl.getUserLikes);
  125. apiRoutes.route('/users/id/like/:userid')
  126. .post(userCtrl.likeUser);
  127. apiRoutes.route('/users/id/unlike/:userid')
  128. .post(userCtrl.unlikeUser);
  129. //FINS AQUÏ COMPROVAT
  130. apiRoutes.route('/travels/comment/:travelid')
  131. .get(travelCtrl.getCommentsByTravelId);
  132. /*apiRoutes.route('/travels/join/:travelId')
  133. .post(travelCtrl.addJoin);
  134. apiRoutes.route('/travels/unjoin/:travelId')
  135. .post(travelCtrl.doUnjoin);*/
  136. /*apiRoutes.route('/travels/:travelId/join')
  137. .post(travelCtrl.addJoin);
  138. apiRoutes.route('/travels/:travelId/unjoin')
  139. .post(travelCtrl.doUnjoin);
  140. */
  141. apiRoutes.route('/users/:userId/fav')
  142. .post(userCtrl.addFav);
  143. apiRoutes.route('/users/:userId/unfav')
  144. .post(userCtrl.doUnfav);
  145. apiRoutes.route('/travels/:travelId/comment')
  146. .post(travelCtrl.addComment);
  147. app.use('/api', apiRoutes);
  148. // end of API routes -------------------------------------
  149. // Start server
  150. app.listen(config.port, function() {
  151. console.log("Node server running on http://localhost:3000");
  152. });