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.

116 lines
3.5 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. // route middleware to verify a token
  58. apiRoutes.use(function(req, res, next) {
  59. // check header or url parameters or post parameters for token
  60. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  61. // decode token
  62. if (token) {
  63. // verifies secret and checks exp
  64. jwt.verify(token, app.get('superSecret'), function(err, decoded) {
  65. if (err) {
  66. return res.send(204, {
  67. success: false,
  68. message: 'Failed to authenticate token.'
  69. });
  70. } else {
  71. // if everything is good, save to request for use in other routes
  72. req.decoded = decoded;
  73. //console.log("decoded " + decoded);
  74. next();
  75. }
  76. });
  77. } else {
  78. // if there is no token
  79. // return an error
  80. return res.status(204).send({
  81. success: false,
  82. message: 'No token provided.'
  83. });
  84. }
  85. }); //fi verificació de token
  86. apiRoutes.route('/events')
  87. .post(eventCtrl.addEvent);
  88. apiRoutes.route('/alerts')
  89. .post(eventCtrl.addAlert);
  90. apiRoutes.route('/events/id/:eventid')
  91. .delete(eventCtrl.deleteEvent);
  92. app.use('/api', apiRoutes);
  93. // end of API routes -------------------------------------
  94. // Start server
  95. app.listen(config.port, function() {
  96. console.log("Node server running on http://localhost:3000");
  97. });