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.

148 lines
4.3 KiB

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