Creating the Comments Server

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.

Listing 27.4 comments_server.js: Implementing the comment application server by using Express and connecting to MongoDB


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);


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.160.92