src="
.12.4/jquery.min.js"> </script> </head> <body> <div id="catalog"> <p>James F. Kurose and Keith W. Ross, Computer Networking: A Approach (6th Edition), Pearson, 2012.</p> <p id="copies">Copies ordered: 0</p> <p id="tocart"> Add t o cart</p> </div> <script> Page 15of18
Top-Down

Question 5 (18%) You are given the following Node.js code implementing a web application. (1) (2%) Describe what this web application is (2) (16%) In app.js and contacts.js below, some chunks of code are highlighted (the code chunks are separated by empty lines): there are 5 highlighted code chunks in app.js, and 3 highlighted code chunks in contacts.js. State what each highlighted chunk of code does, in the space to the right the code.
about.
of
Page
16of18

var app
=e
xpress();
app.
use(
logger('dev'));
a
pp
. use(bodyPa rser.json
()
);
app.use(bodyParser.urlencoded( {extended:
false}));
app.
use(
ex
press
.static(path
.join(_
di
rna
me,
'public')));
app.use(function(req,res,next){
req.db
=db
;
next();
})
;
var contacts= require('./routes/contacts');
app.use('/contacts', contacts);
module.
exports=
app;
routes/
contacts.js
var
express= require('express');
var
router=
express.Router();
router.get('/getcontactlist', function(req, r
es)
{
var
db=
req.db;
});
var collection= db.get('contactlist');
collection.find({}, ['name'], function(e
,d
ocs){
res
.json(docs);
});
router
.get('/getcontact/:id', function(req,
res)
{
var
db=
req.db;
var collection= db.get('contactlist');
var contactlD = req.params.id;
collection. find ({'_id ':contact! D
},
{},function( e,docs){
res.json(docs);
}
);
})
;
Page
17of
18

router.post('/addcontact', function(req, res) {
var
db=
req.db;
var collecti
on=
db.get('contactlist');
collection. insert( req. body, function (err,
result){
res.send((err =
==null)?
{ msg:
"}:
{ msg:
err});
});
});
router.put('/updatecontact/:id',
function(req,
res)
var
db=
req.
db;
var
collection.= db.get('contactlist');
var contactlD =
req.paramq~;
var updateValue = req.body;
collection.update( {'_id' :contactlD},
updateValue, function(err, result){
res.send((err
===null)?
{ msg:
"}:
{ msg:
err})
;
}
);
});
router.delete('/de letecontact/:id
',
function( req,
res)
{
var
db=
req.db;
var contactlD = req.params.id;
var
co
llect
ion=
db
.get('contactlist');
co
llection.remove( {'_id' :contact!
D}
,
function(err, resul
t){
}
);
res.send((err === null)?{msg:"}:{msg:err});
});
module.exports= router;
--
END
OF
PAPER
--
Page
18of1
8

You've reached the end of your free preview.
Want to read all 18 pages?
- Fall '13
- Dr. C. Wu
- Computer Science, IP address, Web page, World Wide Web, Web server