Skip to content
Permalink
Browse files

Current to Part 4 of the blog series

  • Loading branch information...
bmajewski committed Feb 20, 2015
1 parent d82adb4 commit 18b0d51ee45d966abf9293be9a5fe25c6af8c6a6
Showing with 89 additions and 3 deletions.
  1. +11 −0 app/models/user.js
  2. +69 −0 app/routes/user.js
  3. +1 −0 package.json
  4. +8 −3 server.js
@@ -0,0 +1,11 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var UserSchema = new Schema({
name: String,
email: {type: String, required: true, index: {unique: true}},
password: {type: String, required: true, select: false}
});

module.exports = mongoose.model('User', UserSchema);
@@ -0,0 +1,69 @@
var bodyParser = require('body-parser');
var User = require('../models/user');

module.exports = function (app, express) {
var userRouter = express.Router();

userRouter.get('/', function (req, res) {
res.json({message: 'api is loaded'});
});

userRouter.route('/users')
.post(function (req, res) {
var user = new User();
user.name = req.body.name;
user.email = req.body.email;
user.password = req.body.password;

user.save(function (err) {
if (err) {
if (err.code === 11000) {
return res.json({success: false, message: 'Duplicate username.'});
} else {
return res.send(err);
}
} else {
res.json({message: 'user created'});
}

});
})
.get(function (req, res) {
User.find(function (err, users) {
if (err) {
res.send(err);
}
res.json(users);
})
});

userRouter.route('/users/:user_id')
.get(function (req, res) {
User.findById(req.params.user_id, function (err, user) {
if (err) res.send(err);
res.json(user);
})
})
.put(function (req, res) {
User.findById(req.params.user_id, function (err, user) {
if (err) res.send(err);

if (req.body.name) user.name = req.body.name;
if (req.body.username) user.username = req.body.username;
if (req.body.password) user.password = req.body.password;

user.save(function (err) {
if (err)res.send(err);
res.json({message: 'user updated'});
});
});
})
.delete(function (req, res) {
User.remove({_id: req.params.user_id}, function (err, user) {
if (err) res.send(err);
res.json({message: 'user deleted'});
})
});

return userRouter;
};
@@ -11,6 +11,7 @@
"dependencies": {
"body-parser": "^1.12.0",
"express": "^4.11.2",
"mongoose": "^3.8.23",
"morgan": "^1.5.1"
}
}
@@ -1,21 +1,26 @@
var express = require('express');
var bodyParser = require('body-parser');
var morgan = require('morgan');

var mongoose = require('mongoose');
var path = require('path');

mongoose.connect('mongodb://localhost:27017/rlbb');

var app = express();

// Sets us up to read body content from POST requests
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

// Adds request logging
app.use(morgan('dev'));

// Sets up the server to serve static files from the public directory
app.use(express.static(__dirname + '/public'));

var userRoutes = require('./app/routes/user')(app, express);
app.use('/api', userRoutes);

// For all requests we do not explicitly handle (*), return index.html
app.get('*', function(req,res){
res.sendFile(path.join(__dirname + '/public/index.html'));

0 comments on commit 18b0d51

Please sign in to comment.
You can’t perform that action at this time.