With the model defined, you can begin implementing the comments server. Listing 27.4 implements the Express server for the comments application. This code should be familiar to you. It includes the express
and mongoose
libraries and connects to MongoDB via Mongoose.
Notice that there is a require
statement for each of the model definitions to build the Schema
object within Mongoose. Also, the ./comment_routes
file is included to initialize the routes for the server before listening on port 80.
01 var express = require('express'),
02 var bodyParser = require('body-parser'),
03 var mongoose = require('mongoose'),
04 var db = mongoose.connect('mongodb://localhost/comments'),
05 require('./models/comments_model.js'),
06 require('./models/photo_model.js'),
07 require('./models/page_model.js'),
08 var app = express();
09 app.engine('.html', require('ejs').__express);
10 app.set('views', __dirname + '/views'),
11 app.set('view engine', 'html'),
12 app.use(bodyParser());
13 require('./comment_routes')(app);
14 app.listen(80);