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.

125 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({ 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 travelCtrl = require('./controllers/travelController');
  27. /*// Example Route
  28. var router = express.Router();
  29. router.get('/', function(req, res) {
  30. res.send("Hello world!");
  31. });
  32. app.use(router);*/
  33. app.use(express.static(__dirname + '/web'));
  34. //CORS
  35. app.use(function(req, res, next) {
  36. res.header("Access-Control-Allow-Origin", "*");
  37. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  38. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token");
  39. next();
  40. });
  41. // API routes ------------------------------------------------------
  42. var apiRoutes = express.Router();
  43. apiRoutes.route('/users')
  44. .get(userCtrl.findAllUsers)
  45. .post(userCtrl.addUser);
  46. apiRoutes.route('/users/:id')
  47. .get(userCtrl.findById);
  48. apiRoutes.route('/users/byusername/:username')
  49. .get(userCtrl.findUserByUsername);
  50. apiRoutes.route('/travels/user/:username')
  51. .get(travelCtrl.findAllTravelsFromUsername);
  52. apiRoutes.route('/auth')
  53. .post(userCtrl.login);
  54. apiRoutes.route('/travels')
  55. .get(travelCtrl.findAllTravels);
  56. apiRoutes.route('/travels/:id')
  57. .get(travelCtrl.findById);
  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({ success: false, message: 'Failed to authenticate token.' });
  69. } else {
  70. // if everything is good, save to request for use in other routes
  71. req.decoded = decoded;
  72. //console.log("decoded " + decoded);
  73. next();
  74. }
  75. });
  76. } else {
  77. // if there is no token
  78. // return an error
  79. return res.status(201).send({
  80. success: false,
  81. message: 'No token provided.'
  82. });
  83. }
  84. }); //fi verificació de token
  85. apiRoutes.route('/users/:id')
  86. .put(userCtrl.updateUser)
  87. .delete(userCtrl.deleteUser);
  88. apiRoutes.route('/travels')
  89. .post(travelCtrl.addTravel);
  90. apiRoutes.route('/travels/:id')
  91. .put(travelCtrl.updateTravel)
  92. .delete(travelCtrl.deleteTravel);
  93. app.use('/api', apiRoutes);
  94. // end of API routes -------------------------------------
  95. // Start server
  96. app.listen(config.port, function() {
  97. console.log("Node server running on http://localhost:3000");
  98. });