-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
163 lines (128 loc) · 3.29 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// derived from:
// https://github.com/addyosmani/backbone-boilerplates/blob/master/option2/app.js
var application_root = __dirname,
express = require("express"),
path = require("path"),
jsdom = require('jsdom'),
request = require('request'),
mongoose = require('mongoose'),
connection_string = require('./connection_string');
var app = express();
// model
mongoose.connect(connection_string);
var Link = mongoose.model('Link', new mongoose.Schema({
name: String,
url: String,
tags: String
}));
var Tag = mongoose.model('Tag', new mongoose.Schema({
name: String
}));
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(application_root));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
app.get('/links', function(req, res){
res.sendfile(__dirname + '/index.html');
});
// *** RESTful API section ***
// Get all
app.get('/api/tags', function(req, res){
return Tag.find(function(err, data) {
return res.send(data);
});
});
app.get('/api/links', function(req, res){
return Link.find(function(err, data) {
return res.send(data);
});
});
// Get by id
app.get('/api/tag/:id', function(req, res){
return Tag.findById(req.params.id, function(err, tag) {
if (!err) {
return res.send(tag);
}
});
});
app.get('/api/link/:id', function(req, res){
return Link.findById(req.params.id, function(err, link) {
if (!err) {
return res.send(link);
}
});
});
// Save (post)
app.post('/api/tag', function(req, res){
var tag;
tag = new Tag({
name: req.body.name
});
tag.save(function(err) {
if (!err) {
return console.log("created");
}
});
return res.send(tag);
});
app.post('/api/link', function(req, res){
var link;
var nodeio = require('node.io'), options = {timeout: 10, url: req.body.url};
var job = new nodeio.Job(options, {
input: false,
run: function () {
this.getHtml(options.url, function (err, $) {
var result = $('title').text;
link = new Link({
name: result,
url: req.body.url,
tags: req.body.tags
});
link.save(function(err) {
if (!err) {
return console.log("created");
} else {
console.log(err);
}
});
return res.send(link);
//this.emit(result);
});
}
});
job.run();
//console.log(app.getTitle(req.body.url), 'xxxxxx');
//console.log('dsdsdsd', app.getTitle(req.body.url) );
//link = new Link({
// //name: app.getTitle(req.body.url),
// url: req.body.url,
// tags: req.body.tags
//});
//link.save(function(err) {
// if (!err) {
// return console.log("created");
// }
//});
});
// TODO: put, delete
//app.put('/api/todos/:id', function(req, res){
// return Todo.findById(req.params.id, function(err, todo) {
// todo.text = req.body.text;
// todo.done = req.body.done;
// todo.order = req.body.order;
// return todo.save(function(err) {
// if (!err) {
// console.log("updated");
// }
// return res.send(todo);
// });
// });
//});
//
app.listen(3000);