Book Image

Web Development with MongoDB and Node.js

By : Jason Krol
Book Image

Web Development with MongoDB and Node.js

By: Jason Krol

Overview of this book

This book is designed for developers of any skill level that want to get up and running using Node.js and MongoDB to build full featured web applications. A basic understanding of JavaScript and HTML is the only requirement for this book.
Table of Contents (14 chapters)
12
12. Popular Node.js Web Frameworks
13
Index

Defining the schema and models


For the purposes of the application we are building, we're really only going to have two different unique schemas and associated models: an image model and comment model. If we were to take this application to production and really build it out with all of the necessary features, we should expect to have many more models as well.

First, create a new directory in your project labeled models and we will store the Node.js modules for each of our models here. Create three files in this directory named image.js, comment.js, and index.js. Let's take a look at the image model first. Copy the following block of code into the models/image.js file:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    path = require('path');

var ImageSchema = new Schema({
    title:          { type: String },
    description:    { type: String },
    filename:       { type: String },
    views:          { type: Number, 'default': 0 },
    likes:          { type: Number...