Browse Source

post with token runs now

pull/2/head
nau 8 years ago
parent
commit
b852af593e
5 changed files with 28 additions and 15 deletions
  1. +4
    -2
      config.js
  2. +1
    -3
      controllers/travelController.js
  3. +13
    -1
      controllers/userController.js
  4. +7
    -7
      package.json
  5. +3
    -2
      server.js

+ 4
- 2
config.js

@ -1,7 +1,9 @@
module.exports = { module.exports = {
'secret': process.env.SECRET,
'database': process.env.MONGO_DSN,
/*'secret': process.env.SECRET,
'database': process.env.MONGO_DSN,*/
'secret': 'secretfortoken',
'database': 'mongodb://localhost/comunalcar',
"port" : process.env.PORT || 3000 "port" : process.env.PORT || 3000
}; };

+ 1
- 3
controllers/travelController.js

@ -47,9 +47,7 @@ exports.findAllTravelsFromUsername = function(req, res) {
}; };
exports.addTravel = function(req, res) { exports.addTravel = function(req, res) {
console.log('POST new travel, content: ' + req.body.content);
console.log(req.body);
console.log('POST new travel, title: ' + req.body.title);
var travel = new travelModel({ var travel = new travelModel({
title: req.body.title, title: req.body.title,
description: req.body.description, description: req.body.description,

+ 13
- 1
controllers/userController.js

@ -15,6 +15,13 @@ exports.findAllUsers = function(req, res) {
userModel.find(function(err, users) { userModel.find(function(err, users) {
if(err) res.send(500, err.message); if(err) res.send(500, err.message);
//password deletion
for(var i=0; i<users.length; i++)
{
users[i].password="";
console.log(users[i].password);
}
console.log('GET /users'); console.log('GET /users');
res.status(200).jsonp(users); res.status(200).jsonp(users);
}); });
@ -26,6 +33,9 @@ exports.findById = function(req, res) {
if(err) return res.send(500, err.message); if(err) return res.send(500, err.message);
console.log('GET /users/' + req.params.id); console.log('GET /users/' + req.params.id);
//password deletion
user.password="";
res.status(200).jsonp(user); res.status(200).jsonp(user);
}); });
}; };
@ -40,9 +50,10 @@ exports.findUserByUsername = function(req, res) {
if (!user) { if (!user) {
res.json({ success: false, message: 'no user found' }); res.json({ success: false, message: 'no user found' });
} else if (user) { } else if (user) {
console.log(user);
// return the information including token as JSON // return the information including token as JSON
//res.jsonp(user); //res.jsonp(user);
user.password="";
console.log(user);
res.status(200).jsonp(user[0]); res.status(200).jsonp(user[0]);
@ -85,6 +96,7 @@ exports.updateUser = function(req, res) {
user.save(function(err) { user.save(function(err) {
if(err) return res.send(500, err.message); if(err) return res.send(500, err.message);
user.password="";
res.status(200).jsonp(user); res.status(200).jsonp(user);
}); });
}); });

+ 7
- 7
package.json

@ -4,14 +4,14 @@
"description": "comunalCar, carsharing", "description": "comunalCar, carsharing",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
"start": "node server.js"
"start": "node server.js"
}, },
"dependencies": { "dependencies": {
"mongoose": "^4.5.6",
"express": "^4.7.1",
"method-override": "^2.1.2",
"body-parser": "^1.5.1",
"jsonwebtoken" : "latest",
"morgan" : "latest"
"body-parser": "latest",
"express": "^4.7.1",
"jsonwebtoken": "latest",
"method-override": "^2.1.2",
"mongoose": "latest",
"morgan": "latest"
} }
} }

+ 3
- 2
server.js

@ -9,7 +9,7 @@ var morgan = require('morgan');
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens 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;
// Connection to DB // Connection to DB
mongoose.connect(config.database, function(err, res) { mongoose.connect(config.database, function(err, res) {
if(err) throw err; if(err) throw err;
@ -45,7 +45,7 @@ app.use(express.static(__dirname + '/web'));
app.use(function(req, res, next) { app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-Access-Token");
next(); next();
}); });
@ -88,6 +88,7 @@ apiRoutes.use(function(req, res, next) {
} else { } else {
// if everything is good, save to request for use in other routes // if everything is good, save to request for use in other routes
req.decoded = decoded; req.decoded = decoded;
//console.log("decoded " + decoded);
next(); next();
} }
}); });

Loading…
Cancel
Save