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.

155 lines
4.7 KiB

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