-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.js
68 lines (54 loc) · 1.61 KB
/
route.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// load up the page model
var Page = require('./models/page');
// this is how you export the module in the current js file
module.exports = function(app) {
// app.get(path, callback [, callback ...])
// Routes HTTP GET requests to the specified path with the specified callback functions
app.get('/',function(req,res) {
res.render('index');
});
app.get('/new',function(req,res) {
res.render('new');
});
app.get('/page/:name',function(req,res) {
Page.findOne({'content.name':req.params.name},function(err,page){
res.render('template',{
page:page
});
})
});
app.post('/create',function(req,res) {
Page.findOne({'content.name':req.body.name},function(err,page) {
// if there is an error, stop everything and return that
// ie an error connecting to the database
if (err)
throw err;
// if the page is found
if (page) {
var url='/page/'+page.content.name;
res.redirect(url);
}
else {
var newPage = new Page();
newPage.content.name=req.body.name;
newPage.content.major=req.body.major;
newPage.content.school=req.body.school;
var interests=req.body.interests.split(',');
for (i=0;i<interests.length;i++) {
newPage.content.interests.push(interests[i]);
}
newPage.content.summary=req.body.summary;
newPage.content.life=req.body.life;
newPage.content.accomplishments=req.body.accomplishments;
newPage.content.references=req.body.references;
newPage.save(function(err) {
if (err)
throw err;
url='/page/'+req.body.name;
console.log(url);
res.redirect(url);
});
}
});
});
}