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.

133 lines
3.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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 projectMdl = require('./models/projectModel')(app, mongoose);
  26. var projectCtrl = require('./controllers/projectController');
  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 + '/webapp'));
  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. .post(userCtrl.addUser);
  45. apiRoutes.route('/auth')
  46. .post(userCtrl.login);
  47. // OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
  48. // route middleware to verify a token
  49. apiRoutes.use(function(req, res, next) {
  50. // check header or url parameters or post parameters for token
  51. var token = req.body.token || req.query.token || req.headers['x-access-token'];
  52. // decode token
  53. if (token) {
  54. // verifies secret and checks exp
  55. jwt.verify(token, app.get('superSecret'), function(err, decoded) {
  56. if (err) {
  57. return res.json({ success: false, message: 'Failed to authenticate token.' });
  58. } else {
  59. // if everything is good, save to request for use in other routes
  60. req.decoded = decoded;
  61. //console.log("decoded " + decoded);
  62. next();
  63. }
  64. });
  65. } else {
  66. // if there is no token
  67. // return an error
  68. return res.status(201).send({
  69. success: false,
  70. message: 'No token provided.'
  71. });
  72. }
  73. }); //fi verificació de token
  74. apiRoutes.route('/logout')
  75. .post(userCtrl.logout);
  76. apiRoutes.route('/users')
  77. .get(userCtrl.findAllUsers);
  78. apiRoutes.route('/users/:id')
  79. .get(userCtrl.findById);
  80. apiRoutes.route('/users/byusername/:username')
  81. .get(userCtrl.findUserByUsername);
  82. apiRoutes.route('/projects/user/:username')
  83. .get(projectCtrl.findAllProjectsFromUsername);
  84. apiRoutes.route('/projects')
  85. .get(projectCtrl.findAllProjects);
  86. apiRoutes.route('/projects/:id')
  87. .get(projectCtrl.findById);
  88. apiRoutes.route('/users/:id')
  89. .put(userCtrl.updateUser)
  90. .delete(userCtrl.deleteUser);
  91. apiRoutes.route('/projects')
  92. .post(projectCtrl.addProject);
  93. apiRoutes.route('/projects/:id')
  94. .put(projectCtrl.updateProject)
  95. .delete(projectCtrl.deleteProject);
  96. app.use('/api', apiRoutes);
  97. // end of API routes -------------------------------------
  98. // Start server
  99. app.listen(config.port, function() {
  100. console.log("Node server running on http://localhost:3000");
  101. });