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.

124 lines
3.4 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. // Connection to DB
  10. mongoose.connect(config.database, function(err, res) {
  11. if(err) throw err;
  12. console.log('Connected to Database');
  13. });
  14. app.set('superSecret', config.secret); // secret variable
  15. // Middlewares
  16. app.use(bodyParser.urlencoded({ extended: false }));
  17. app.use(bodyParser.json());
  18. app.use(methodOverride());
  19. // use morgan to log requests to the console
  20. app.use(morgan('dev'));
  21. // Import Models and controllers
  22. var userMdl = require('./models/userModel')(app, mongoose);
  23. var userCtrl = require('./controllers/userController');
  24. var travelMdl = require('./models/travelModel')(app, mongoose);
  25. var travelCtrl = require('./controllers/travelController');
  26. /*// Example Route
  27. var router = express.Router();
  28. router.get('/', function(req, res) {
  29. res.send("Hello world!");
  30. });
  31. app.use(router);*/
  32. app.use(express.static(__dirname + '/web'));
  33. //CORS
  34. app.use(function(req, res, next) {
  35. res.header("Access-Control-Allow-Origin", "*");
  36. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  37. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  38. next();
  39. });
  40. // API routes ------------------------------------------------------
  41. var apiRoutes = express.Router();
  42. apiRoutes.route('/users')
  43. .get(userCtrl.findAllUsers)
  44. .post(userCtrl.addUser);
  45. apiRoutes.route('/users/:id')
  46. .get(userCtrl.findById);
  47. apiRoutes.route('/users/byusername/:username')
  48. .get(userCtrl.findUserByUsername);
  49. apiRoutes.route('/travels/user/:username')
  50. .get(travelCtrl.findAllTravelsFromUsername);
  51. apiRoutes.route('/auth')
  52. .post(userCtrl.login);
  53. apiRoutes.route('/travels')
  54. .get(travelCtrl.findAllTravels);
  55. apiRoutes.route('/travels/:id')
  56. .get(travelCtrl.findById)
  57. /* OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
  58. // route middleware to verify a token
  59. apiRoutes.use(function(req, res, next) {
  60. // check header or url parameters or post parameters for token
  61. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  62. // decode token
  63. if (token) {
  64. // verifies secret and checks exp
  65. jwt.verify(token, app.get('superSecret'), function(err, decoded) {
  66. if (err) {
  67. return res.json({ success: false, message: 'Failed to authenticate token.' });
  68. } else {
  69. // if everything is good, save to request for use in other routes
  70. req.decoded = decoded;
  71. next();
  72. }
  73. });
  74. } else {
  75. // if there is no token
  76. // return an error
  77. return res.status(403).send({
  78. success: false,
  79. message: 'No token provided.'
  80. });
  81. }
  82. });*/
  83. apiRoutes.route('/users/:id')
  84. .put(userCtrl.updateUser)
  85. .delete(userCtrl.deleteUser);
  86. apiRoutes.route('/travels')
  87. .post(travelCtrl.addTravel);
  88. apiRoutes.route('/travels/:id')
  89. .put(travelCtrl.updateTravel)
  90. .delete(travelCtrl.deleteTravel);
  91. app.use('/api', apiRoutes);
  92. // end of API routes -------------------------------------
  93. // Start server
  94. app.listen(config.port, function() {
  95. console.log("Node server running on http://localhost:3000");
  96. });