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.

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