Creating the Shopping Cart Server

With the model defined, you can begin implementing the shopping cart server. Listing 28.7 implements the Express server for the shopping cart 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 the model definition to build the Schema object within Mongoose. Also, the ./cart_routes file is included to initialize the routes for the server before listening on port 80.

Listing 28.7 cart_server.js: Implementing the shopping cart application server 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/cart'),
05 require('./models/cart_model.js'),
06 var app = express();
07 app.engine('.html', require('ejs').__express);
08 app.set('views', __dirname + '/views'),
09 app.set('view engine', 'html'),
10 app.use(bodyParser());
11 require('./cart_routes')(app);
12 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
18.117.71.189