@ -1,24 +1,26 @@
var express = require ( "express" ) ,
app = express ( ) ,
bodyParser = require ( "body-parser" ) ,
methodOverride = require ( "method-override" ) ,
mongoose = require ( 'mongoose' ) ;
var express = require ( "express" ) ,
app = express ( ) ,
bodyParser = require ( "body-parser" ) ,
methodOverride = require ( "method-override" ) ,
mongoose = require ( 'mongoose' ) ;
var morgan = require ( 'morgan' ) ;
var jwt = require ( 'jsonwebtoken' ) ; // used to create, sign, and verify tokens
var morgan = require ( 'morgan' ) ;
var jwt = require ( 'jsonwebtoken' ) ; // used to create, sign, and verify tokens
var config = require ( './config' ) ; // get our config file
var config = require ( './config' ) ; // get our config file
mongoose . Promise = global . Promise ;
mongoose . Promise = global . Promise ;
// Connection to DB
// Connection to DB
mongoose . connect ( config . database , function ( err , res ) {
mongoose . connect ( config . database , function ( err , res ) {
if ( err ) throw err ;
console . log ( 'Connected to Database' ) ;
if ( err ) throw err ;
console . log ( 'Connected to Database' ) ;
} ) ;
} ) ;
app . set ( 'superSecret' , config . secret ) ; // secret variable
app . set ( 'superSecret' , config . secret ) ; // secret variable
// Middlewares
// Middlewares
app . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
app . use ( bodyParser . urlencoded ( {
extended : false
} ) ) ;
app . use ( bodyParser . json ( ) ) ;
app . use ( bodyParser . json ( ) ) ;
app . use ( methodOverride ( ) ) ;
app . use ( methodOverride ( ) ) ;
@ -26,12 +28,11 @@ app.use(methodOverride());
app . use ( morgan ( 'dev' ) ) ;
app . use ( morgan ( 'dev' ) ) ;
// Import Models and controllers
// Import Models and controllers
var userMdl = require ( './models/userModel' ) ( app , mongoose ) ;
var userMdl = require ( './models/userModel' ) ( app , mongoose ) ;
var userCtrl = require ( './controllers/userController' ) ;
var userCtrl = require ( './controllers/userController' ) ;
var travelMdl = require ( './models/travelModel' ) ( app , mongoose ) ;
var joinMdl = require ( './models/joinModel' ) ( app , mongoose ) ;
var commentMdl = require ( './models/commentModel' ) ( app , mongoose ) ;
var travelMdl = require ( './models/travelModel' ) ( app , mongoose ) ;
var commentMdl = require ( './models/commentModel' ) ( app , mongoose ) ;
var travelCtrl = require ( './controllers/travelController' ) ;
var travelCtrl = require ( './controllers/travelController' ) ;
/*/ / Example Route
/*/ / Example Route
@ -45,82 +46,80 @@ app.use(express.static(__dirname + '/www'));
//CORS
//CORS
app . use ( function ( req , res , next ) {
app . use ( function ( req , res , next ) {
res . header ( "Access-Control-Allow-Origin" , "*" ) ;
res . header ( 'Access-Control-Allow-Methods' , 'GET,PUT,POST,DELETE,OPTIONS' ) ;
res . header ( "Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token" ) ;
next ( ) ;
res . header ( "Access-Control-Allow-Origin" , "*" ) ;
res . header ( 'Access-Control-Allow-Methods' , 'GET,PUT,POST,DELETE,OPTIONS' ) ;
res . header ( "Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token" ) ;
next ( ) ;
} ) ;
} ) ;
// API routes ------------------------------------------------------
// API routes ------------------------------------------------------
var apiRoutes = express . Router ( ) ;
var apiRoutes = express . Router ( ) ;
apiRoutes . route ( '/users' )
. get ( userCtrl . findAllUsers )
. post ( userCtrl . addUser ) ;
apiRoutes . route ( '/users/:id' )
. get ( userCtrl . findById ) ;
apiRoutes . route ( '/users/byusername/:username' )
. get ( userCtrl . findUserByUsername ) ;
apiRoutes . route ( '/travels/user/:username' )
. get ( travelCtrl . findAllTravelsFromUsername ) ;
apiRoutes . route ( '/auth' )
apiRoutes . route ( '/login' )
. post ( userCtrl . login ) ;
. post ( userCtrl . login ) ;
apiRoutes . route ( '/signup' )
. post ( userCtrl . signup ) ;
apiRoutes . route ( '/users' )
. get ( userCtrl . getAllUsers ) ;
apiRoutes . route ( '/users/getById/:userid' )
. get ( userCtrl . getUserById ) ;
apiRoutes . route ( '/travels' )
apiRoutes . route ( '/travels' )
. get ( travelCtrl . findAllTravels ) ;
. get ( travelCtrl . getAllTravels ) ;
apiRoutes . route ( '/travels/getById/:travelid' )
. get ( travelCtrl . getTravelById ) ;
apiRoutes . route ( '/travels/:id' )
. get ( travelCtrl . findById ) ;
apiRoutes . route ( '/travels/join/:travelId' )
. get ( travelCtrl . getJoinsByTravelId ) ;
apiRoutes . route ( '/travels/comment/:travelId' )
. get ( travelCtrl . getCommentsByTravelId ) ;
// OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
// OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
// route middleware to verify a token
// route middleware to verify a token
apiRoutes . use ( function ( req , res , next ) {
apiRoutes . use ( function ( req , res , next ) {
// check header or url parameters or post parameters for token
var token = req . body . token || req . query . token || req . headers [ 'x-access-token' ] ;
// decode token
if ( token ) {
// verifies secret and checks exp
jwt . verify ( token , app . get ( 'superSecret' ) , function ( err , decoded ) {
if ( err ) {
return res . json ( { success : false , message : 'Failed to authenticate token.' } ) ;
} else {
// if everything is good, save to request for use in other routes
req . decoded = decoded ;
//console.log("decoded " + decoded);
next ( ) ;
}
} ) ;
} else {
// if there is no token
// return an error
return res . status ( 201 ) . send ( {
success : false ,
message : 'No token provided.'
} ) ;
}
// check header or url parameters or post parameters for token
var token = req . body . token || req . query . token || req . headers [ 'x-access-token' ] ;
// decode token
if ( token ) {
// verifies secret and checks exp
jwt . verify ( token , app . get ( 'superSecret' ) , function ( err , decoded ) {
if ( err ) {
return res . json ( {
success : false ,
message : 'Failed to authenticate token.'
} ) ;
} else {
// if everything is good, save to request for use in other routes
req . decoded = decoded ;
//console.log("decoded " + decoded);
next ( ) ;
}
} ) ;
} else {
// if there is no token
// return an error
return res . status ( 201 ) . send ( {
success : false ,
message : 'No token provided.'
} ) ;
}
} ) ; //fi verificació de token
} ) ; //fi verificació de token
apiRoutes . route ( '/users/:id' )
apiRoutes . route ( '/users/:id' )
. put ( userCtrl . updateUser )
. delete ( userCtrl . deleteUser ) ;
. put ( userCtrl . updateUser )
. delete ( userCtrl . deleteUser ) ;
apiRoutes . route ( '/travels' )
apiRoutes . route ( '/travels' )
. post ( travelCtrl . addTravel ) ;
. post ( travelCtrl . addTravel ) ;
//FINS AQUÏ COMPROVAT
apiRoutes . route ( '/travels/:id' )
apiRoutes . route ( '/travels/:id' )
. put ( travelCtrl . updateTravel )
. delete ( travelCtrl . deleteTravel ) ;
. put ( travelCtrl . updateTravel )
. delete ( travelCtrl . deleteTravel ) ;
apiRoutes . route ( '/travels/addJoin/:travelId' )
. get ( travelCtrl . getJoinsByTravelId ) ;
apiRoutes . route ( '/travels/comment/:travelId' )
. get ( travelCtrl . getCommentsByTravelId ) ;
/ * a p i R o u t e s . r o u t e ( ' / t r a v e l s / j o i n / : t r a v e l I d ' )
/ * a p i R o u t e s . r o u t e ( ' / t r a v e l s / j o i n / : t r a v e l I d ' )
. post ( travelCtrl . addJoin ) ;
. post ( travelCtrl . addJoin ) ;
@ -145,5 +144,5 @@ app.use('/api', apiRoutes);
// Start server
// Start server
app . listen ( config . port , function ( ) {
app . listen ( config . port , function ( ) {
console . log ( "Node server running on http://localhost:3000" ) ;
console . log ( "Node server running on http://localhost:3000" ) ;
} ) ;
} ) ;