> npm install --save mongoose
The above command should update the dependencies of the package.json file, as shown in
the excerpt below:
"dependencies": {
"express": "~4.9.0",
"body-parser": "~1.8.1",
"cookie-parser": "~1.3.3",
"morgan": "~1.3.0",
"serve-favicon": "~2.1.3",
"debug": "~2.0.0",
"jade": "~1.6.0",
"mongoose": "~3.8.20"
}
app_server/models
directory. For now, this file will only have the following command:
var mongoose = require('mongoose');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
require('./app_server/models/db');
var dbURI = 'mongodb://localhost/blogger';
mongoose.connect(dbURI);
Note: - The first time your application successfully connects to the database the database will
be created, if needed.
// Monitor and report when database is connected
mongoose.connection.on('connected', function () {
console.log('Mongoose connected to ' + dbURI);
});
// Monitor and report error connecting to database
mongoose.connection.on('error',function (err) {
console.log('Mongoose connection error: ' + err);
});
// Monitor and report when database is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose disconnected');
});
// Closes (disconnects) from Mongoose DB upon shutdown
gracefulShutdown = function (msg, callback) {
mongoose.connection.close(function () {
console.log('Mongoose disconnected through ' + msg);
callback();
});
};
// For nodemon restarts
process.once('SIGUSR2', function () {
gracefulShutdown('nodemon restart', function () {
process.kill(process.pid, 'SIGUSR2');
}); });
// For app termination
process.on('SIGINT', function() {
gracefulShutdown('app termination', function () {
process.exit(0);
}); });
// For Heroku app termination
process.on('SIGTERM', function() {
gracefulShutdown('Heroku app shutdown', function () {
process.exit(0);
}); });
var mongoose = require( 'mongoose' );
var gracefulShutdown;
var dbURI = 'mongodb://localhost/myapp';
mongoose.connect(dbURI);
// Monitor and report when database is connected
mongoose.connection.on('connected', function () {
console.log('Mongoose connected to ' + dbURI);
});
// Monitor and report error connecting to database
mongoose.connection.on('error',function (err) {
console.log('Mongoose connection error: ' + err);
});
// Monitor and report when database is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose disconnected');
});
// Closes (disconnects) from Mongoose DB upon shutdown
gracefulShutdown = function (msg, callback) {
mongoose.connection.close(function () {
console.log('Mongoose disconnected through ' + msg);
callback();
});
};
// For nodemon restarts
process.once('SIGUSR2', function () {
gracefulShutdown('nodemon restart', function () {
process.kill(process.pid, 'SIGUSR2');
}); });
// For app termination
process.on('SIGINT', function() {
gracefulShutdown('app termination', function () {
process.exit(0);
}); });
// For Heroku app termination
process.on('SIGTERM', function() {
gracefulShutdown('Heroku app shutdown', function () {
process.exit(0);
}); });
var reviewSchema = new mongoose.Schema({
author: String,
rating: {
type: Number,
required: true,
min: 0,
max: 5
},
reviewText: String,
createdOn: {
type: Date,
"default": Date.now
}
});
locations.js
file is
called a model file and it is stored in the models
directory.
locations.js
from Chapter 5 is just such a model, and it contains schemas.
require('./locations');