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.

118 lines
3.6 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({
  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. var userMdl = require('./models/userModel')(app, mongoose);
  25. var userCtrl = require('./controllers/userController');
  26. var eventMdl = require('./models/eventModel')(app, mongoose);
  27. var eventCtrl = require('./controllers/eventController');
  28. //var alertMdl = require('./models/alertModel')(app, mongoose);
  29. //var alertCtrl = require('./controllers/alertController');
  30. //CORS
  31. app.use(function(req, res, next) {
  32. res.header("Access-Control-Allow-Origin", "*");
  33. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  34. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token");
  35. next();
  36. });
  37. // API routes ------------------------------------------------------
  38. var apiRoutes = express.Router();
  39. apiRoutes.route('/login')
  40. .post(userCtrl.login);
  41. apiRoutes.route('/signup')
  42. .post(userCtrl.signup);
  43. apiRoutes.route('/users')
  44. .get(userCtrl.getAllUsers);
  45. apiRoutes.route('/users/id/:userid')
  46. .get(userCtrl.getUserById);
  47. apiRoutes.route('/events')
  48. .get(eventCtrl.getAllEvents);
  49. apiRoutes.route('/alerts')
  50. .get(eventCtrl.getAllAlerts);
  51. apiRoutes.route('/events/id/:eventid')
  52. .get(eventCtrl.getEventById);
  53. apiRoutes.route('/events/following')
  54. .post(eventCtrl.getEventsByFollowingArray);
  55. apiRoutes.route('/events/category/:category')
  56. .get(eventCtrl.getEventsByCategory);
  57. apiRoutes.route('/categoriesList')
  58. .get(eventCtrl.getCategoriesList);
  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.send(204, {
  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(204).send({
  83. success: false,
  84. message: 'No token provided.'
  85. });
  86. }
  87. }); //fi verificació de token
  88. apiRoutes.route('/events')
  89. .post(eventCtrl.addEvent);
  90. apiRoutes.route('/alerts')
  91. .post(eventCtrl.addAlert);
  92. apiRoutes.route('/events/id/:eventid')
  93. .delete(eventCtrl.deleteEvent);
  94. app.use('/api', apiRoutes);
  95. // end of API routes -------------------------------------
  96. // Start server
  97. app.listen(config.port, function() {
  98. console.log("Node server running on http://localhost:3000");
  99. });