-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (84 loc) · 2.51 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
// Module dependencies
const moment = require('moment')
const express = require('express')
const app = express()
const schedule = require('node-schedule')
const { Item } = require('./models')
const logger = require('./logger')
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(require('stylus').middleware({src: __dirname + '/public', compress: true, firebug: true}))
app.use(express.static(__dirname + '/public'), { maxAge: 3600*12*7 })
})
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
})
app.configure('production', function(){
app.use(express.errorHandler())
})
// Routes
// {source:"weibo"},{created_at:1,text:1,image_url:1,author:1},{lim:1}
app.get('/', function(req, res){
Item.find({},{},{limit:1,sort:[['created_at', -1]]},function(err,docs){
res.render('index.jade', {
title: '歌德的理念',
items: docs,
time: moment(docs[0].created_at).fromNow(),
page: 2
})
})
})
app.get('/test', function(req, res){
res.send('just test!')
})
app.get('/callback', function(req, res){
res.send('')
})
app.get('/callbackcancel', function(req, res){
res.send('user cancel!')
})
app.get('/ajax/:id', function(req, res){
Item.find({},{},{limit:1,skip:parseInt(req.params.id)-1,sort:[['created_at', -1]]},function(err,docs){
res.send(docs)
})
})
app.get('/:id', function(req, res){
Item.find({},{},{limit:1,skip:parseInt(req.params.id)-1,sort:[['created_at', -1]]},function(err,docs){
res.render('index.jade', {
title : '歌德的理念',
items : docs,
time : moment(docs[0].created_at).fromNow(),
page : parseInt(req.params.id)+1
})
})
})
app.get('/bak/:id', function(req, res){
Item.find({_id:req.params.id},{},{limit:1},function(err,docs){
res.render('index.jade', {
title : '歌德的理念',
items : docs,
time : moment(docs[0].created_at).fromNow(),
page : 2
})
})
})
app.get('/cache/:id', function(req, res){
Item.find({_id:req.params.id},{},{limit:1},function(err,docs){
res.render('index.jade', {
title : '歌德的理念',
items : docs,
time : moment(docs[0].created_at).fromNow(),
page : 2
})
})
})
const PORT = process.env.NODE_ENV === 'production' ? 80 : 3000
app.listen(PORT, function(res){
logger.log(`Express server listening on ${PORT}`)
// 每天0点0分抓取最新微博
schedule.scheduleJob('0 0 * * *', () => require('./grab'))
})