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.

191 lines
5.9 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. // OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
  67. // route middleware to verify a token
  68. apiRoutes.use(function(req, res, next) {
  69. // check header or url parameters or post parameters for token
  70. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  71. // decode token
  72. if (token) {
  73. // verifies secret and checks exp
  74. jwt.verify(token, app.get('superSecret'), function(err, decoded) {
  75. if (err) {
  76. return res.send(204,
  77. {
  78. success: false,
  79. message: 'Failed to authenticate token.'
  80. });
  81. } else {
  82. // if everything is good, save to request for use in other routes
  83. req.decoded = decoded;
  84. //console.log("decoded " + decoded);
  85. next();
  86. }
  87. });
  88. } else {
  89. // if there is no token
  90. // return an error
  91. return res.status(204).send({
  92. success: false,
  93. message: 'No token provided.'
  94. });
  95. }
  96. }); //fi verificació de token
  97. //admin
  98. apiRoutes.route('/admin/network')
  99. .get(adminCtrl.network);
  100. apiRoutes.route('/search/:searchstring')
  101. .get(searchCtrl.searchByString);
  102. apiRoutes.route('/numnotifications')
  103. .get(userCtrl.getNumNotificationsByToken);
  104. apiRoutes.route('/notifications')
  105. .get(userCtrl.getNotifications);
  106. apiRoutes.route('/users/token')
  107. .get(userCtrl.getUserByToken);
  108. apiRoutes.route('/users')//agafa l'user a partir del token
  109. .put(userCtrl.updateUser)//no comprovat
  110. .delete(userCtrl.deleteUser);
  111. apiRoutes.route('/changePassword')//agafa l'user a partir del token
  112. .put(userCtrl.changePassword);
  113. apiRoutes.route('/users/id/travels/:userid')
  114. .get(userCtrl.getTravelsByUserId);
  115. apiRoutes.route('/travels')
  116. .post(travelCtrl.addTravel);
  117. apiRoutes.route('/travels/id/modify/:travelid')
  118. .put(travelCtrl.updateTravel)//no comprovat
  119. .delete(travelCtrl.deleteTravel);
  120. apiRoutes.route('/travels/join/:travelid')
  121. .post(travelCtrl.addJoinPetition);
  122. apiRoutes.route('/travels/unjoin/:travelid')
  123. .post(travelCtrl.unJoin);
  124. apiRoutes.route('/travels/leave/:travelid')
  125. .post(travelCtrl.leave);
  126. apiRoutes.route('/travels/declineJoin/:travelid')
  127. .post(travelCtrl.declineJoin);
  128. apiRoutes.route('/travels/acceptJoin/:travelid')
  129. .post(travelCtrl.acceptJoin);
  130. apiRoutes.route('/users/id/likes/:userid')
  131. .get(userCtrl.getUserLikes);
  132. apiRoutes.route('/users/id/like/:userid')
  133. .post(userCtrl.likeUser);
  134. apiRoutes.route('/users/id/unlike/:userid')
  135. .post(userCtrl.unlikeUser);
  136. //FINS AQUÏ COMPROVAT
  137. apiRoutes.route('/travels/comment/:travelid')
  138. .get(travelCtrl.getCommentsByTravelId);
  139. /*apiRoutes.route('/travels/join/:travelId')
  140. .post(travelCtrl.addJoin);
  141. apiRoutes.route('/travels/unjoin/:travelId')
  142. .post(travelCtrl.doUnjoin);*/
  143. /*apiRoutes.route('/travels/:travelId/join')
  144. .post(travelCtrl.addJoin);
  145. apiRoutes.route('/travels/:travelId/unjoin')
  146. .post(travelCtrl.doUnjoin);
  147. */
  148. apiRoutes.route('/users/:userId/fav')
  149. .post(userCtrl.addFav);
  150. apiRoutes.route('/users/:userId/unfav')
  151. .post(userCtrl.doUnfav);
  152. apiRoutes.route('/travels/:travelId/comment')
  153. .post(travelCtrl.addComment);
  154. app.use('/api', apiRoutes);
  155. // end of API routes -------------------------------------
  156. // Start server
  157. app.listen(config.port, function() {
  158. console.log("Node server running on http://localhost:3000");
  159. });