Challenge 4 - Mongoose
Video Course on Building Web Applications with Node.jsStart up mongodb locally:
NodeJS mongoose module to interact with the DB:
list all items with find:
Use Mongoose to save and retreive blog articles
- start up mongodb (locally)
- create a nodejs module for mongoose
- connect to mongodb, set up schema, and model, export
- create a route to list all items with find
- create a POST route, create a new item and save it
mongod --dbpath=/Users/bfulop/work/__mongodata --port 27017
// Post.js
// Require mongoose
var mongoose = require('mongoose');
// Configure conenction URL (only needs to happen once per app)
mongoose.connect('mongodb://localhost/test');
// Create a database schema for our Post object, which will describe both it's
// data and it's behavior.
var postSchema = mongoose.Schema({
title:String,
content:String
});
// Create a model object constructor that will have ODM functionality like .save()...
var Post = mongoose.model('Post', postSchema);
// Expose out model as the module interface
module.exports = Post;
Use this module in app.js:var Post = require('./Post');
// Render our home page with all blog posts
app.get('/', function(request, response) {
Post.find(function(err, posts) {
if (err) {
response.send(500, 'There was an error - tough luck.');
}
else {
response.render('index', {
posts:posts
});
}
});
});
Create a post route, create a new item and save it. Get the post data from the request.body
object:
// create a new blog post object
app.post('/create', function(request, response) {
//Create and save a Post model
var post = new Post({
title: request.body.title,
content: request.body.content
});
//Save the model
post.save(function(err, model) {
if (err) {
response.send(500, 'There was an error - tough luck.');
}
else {
response.redirect('/');
}
});
});
the HTML for the form to POST:
<form action="/create" method="POST">
<label>Post Title</label>
<input type="text" name="title" placeholder="Type a title..."/>
<label>Post Content</label>
<textarea name="content" rows="5"></textarea>
<p>
<button type="submit" class="btn">Submit</button>
</p>
</form>
explains package.json file, most notable the dependencies
section
to pull down all the dependencies: $npm install
a shortcut if you experiment with something to add to package.json :
npm install twilio --save
its will add an entry to the package.json fileUse authentication
basic authentication, middleware
var auth = express.basicAuth(function(username, password){
return username === "foo" && password === "bar";
})
Then just add the auth
middleware we created to our routes:app.post('/create', auth, function(request, response) {...
or if you want to apply it to every route:
app.use(
auth
);
Create an API for listing blog posts as JSON
just use
response.send({///...}
instead of response.render
the
send
will automatically serialise to json:
app.get('/posts.json', function(request, response) {
Post.find(function(err, posts) {
if (err) {
response.send(500, {
success: false
});
}
else {
response.send({
success: true,
posts: posts
});
}
});
});