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.

139 lines
4.1 KiB

  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({ extended: false }));
  18. app.use(bodyParser.json());
  19. app.use(methodOverride());
  20. // use morgan to log requests to the console
  21. app.use(morgan('dev'));
  22. // Import Models and controllers
  23. var userMdl = require('./models/userModel')(app, mongoose);
  24. var userCtrl = require('./controllers/userController');
  25. var travelMdl = require('./models/travelModel')(app, mongoose);
  26. var joinMdl = require('./models/joinModel')(app, mongoose);
  27. var commentMdl = require('./models/commentModel')(app, mongoose);
  28. var travelCtrl = require('./controllers/travelController');
  29. /*// Example Route
  30. var router = express.Router();
  31. router.get('/', function(req, res) {
  32. res.send("Hello world!");
  33. });
  34. app.use(router);*/
  35. app.use(express.static(__dirname + '/web'));
  36. //CORS
  37. app.use(function(req, res, next) {
  38. res.header("Access-Control-Allow-Origin", "*");
  39. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  40. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token");
  41. next();
  42. });
  43. // API routes ------------------------------------------------------
  44. var apiRoutes = express.Router();
  45. apiRoutes.route('/users')
  46. .get(userCtrl.findAllUsers)
  47. .post(userCtrl.addUser);
  48. apiRoutes.route('/users/:id')
  49. .get(userCtrl.findById);
  50. apiRoutes.route('/users/byusername/:username')
  51. .get(userCtrl.findUserByUsername);
  52. apiRoutes.route('/travels/user/:username')
  53. .get(travelCtrl.findAllTravelsFromUsername);
  54. apiRoutes.route('/auth')
  55. .post(userCtrl.login);
  56. apiRoutes.route('/travels')
  57. .get(travelCtrl.findAllTravels);
  58. apiRoutes.route('/travels/:id')
  59. .get(travelCtrl.findById);
  60. apiRoutes.route('/travels/join/:travelId')
  61. .get(travelCtrl.getJoinsByTravelId);
  62. apiRoutes.route('/travels/comment/:travelId')
  63. .get(travelCtrl.getCommentsByTravelId);
  64. // OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
  65. // route middleware to verify a token
  66. apiRoutes.use(function(req, res, next) {
  67. // check header or url parameters or post parameters for token
  68. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  69. // decode token
  70. if (token) {
  71. // verifies secret and checks exp
  72. jwt.verify(token, app.get('superSecret'), function(err, decoded) {
  73. if (err) {
  74. return res.json({ success: false, message: 'Failed to authenticate token.' });
  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(201).send({
  86. success: false,
  87. message: 'No token provided.'
  88. });
  89. }
  90. }); //fi verificació de token
  91. apiRoutes.route('/users/:id')
  92. .put(userCtrl.updateUser)
  93. .delete(userCtrl.deleteUser);
  94. apiRoutes.route('/travels')
  95. .post(travelCtrl.addTravel);
  96. apiRoutes.route('/travels/:id')
  97. .put(travelCtrl.updateTravel)
  98. .delete(travelCtrl.deleteTravel);
  99. apiRoutes.route('/travels/join/:travelId')
  100. .post(travelCtrl.addJoin);
  101. apiRoutes.route('/travels/unjoin/:travelId')
  102. .post(travelCtrl.doUnjoin);
  103. apiRoutes.route('/travels/comment/:travelId')
  104. .post(travelCtrl.addComment);
  105. app.use('/api', apiRoutes);
  106. // end of API routes -------------------------------------
  107. // Start server
  108. app.listen(config.port, function() {
  109. console.log("Node server running on http://localhost:3000");
  110. });