-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
112 lines (101 loc) · 3.33 KB
/
app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var MongoClient = require("mongodb").MongoClient;
var db, env;
if (!process.env.NODE_ENV) { env = require('node-env-file')(__dirname + '/.env'); }
var mongoUrl = process.env.bluemix_mongo_connection || process.env.mongodb_auth;
app.set("view engine", "ejs");//模板渲染引擎设置为ejs
app.use(express.static(__dirname + '/public'));
// need to make sure mongo connectivity before opening web service
MongoClient.connect(mongoUrl, function(err, database) {
if (err) return console.log("error : " + err);
db = database;
app.listen( process.env.VCAP_APP_PORT || 80, function() {
console.log("server started");
});
});
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
// where the app starts from user
app.get("/", function(req, res) {
res.render("index");//主页入口
});
// router for create new category (with website name, url, and icon)
app.get("/category-create", function (req, res) {
res.render("category-create");
});
// router to get all category list by UI view(readonly)
app.get("/category-index", function(req, res) {
res.render("category-index");
});
// router for login with json support, and record those logs!
app.post("/login", function(req, res) {
// retrieve current time
var now = (new Date()).toLocaleString();
var store = { "user": req.body.user_name || "test", "time": now };
// save to database
db.collection("login").save(store, function(err, result) {
res.json({ "status" : "success", "user" : req.body.user_name || "test", "time": now });
});
});
// router for save user behavior and resposne with relevant contents
app.post("/picker-save", function(req, res) {
// save user behavior first
// then retreive related content with selected categories and send back thru json
var cursor = db.collection("content").find({"$or":req.body.channelselect_id });//.sort({"_id": 1});
cursor.toArray(function(err, results) {
var output = {};
output.feed = results;
res.json(output);
});
});
app.get("/homepage-channels",function(req,res) {
var cursor = db.collection("homepage_channels").find();
cursor.toArray(function(err,results){
var output = {};
output.channels = results;
res.json(output);
});
});
app.get("/homepage-search",function(req,res) {
var cursor = db.collection("homepage_search").find();
cursor.toArray(function(err,results){
var output = {};
output.recommend = results;
res.json(output);
});
});
// router to get all category list by JSON
app.get("/category-list", function(req, res) {
var cursor = db.collection("category").find();
cursor.toArray(function(err, results) {
var output = {};
output.feed = results;
res.json(output);
});
});
// router to save category creation点击开始提交的数据存入数据库
app.post("/category-save", function (req, res) {
// init data object
var r = req.body,
l = r.site.length;
s = {};
s.catagory = r.catagory;
s.site = [];
// create site array [name, url, icon]
for (var i=0; i < l ; i++) {
var o = {
"name" : r.site[i],
"url" : r.url[i],
"icon" : r.icon[i]
};
s.site.push(o);
};
// restore in database
db.collection("category").save(s, function(err, result) {
if (err) return console.log(err);
res.redirect("/category-index");
});
});