-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
71 lines (65 loc) · 1.88 KB
/
server.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
const express = require('express')
const app = express()
const AV = require('leanengine')
// setting
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
// res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
// use leancloud
AV.init({
appId: process.env.LEANCLOUD_APP_ID,
appKey: process.env.LEANCLOUD_APP_KEY,
masterKey: process.env.LEANCLOUD_APP_MASTER_KEY
})
app.use(AV.express())
// root router
app.get('/', (req, res) => res.send('hello world !'))
// movie-canlendar
app.get('/movie-calendar', function (req, res) {
const year = req.query.year
const month = req.query.month
const date = req.query.date
const movieCalendarQuery = new AV.Query('moon_data')
if (year === undefined || +year !== 2017) {
res.send({
error: 'year is undefined or is not legal'
})
return false
}
if (month === undefined || +month < 1 || +month > 12) {
res.send({
error: 'month is undefined or is not legal'
})
return false
}
if (date === undefined || +date < 1 || +month > 31) {
res.send({
error: 'date is undefined or is not legal'
})
return false
}
movieCalendarQuery.equalTo('date', month + '月' + date + '日')
movieCalendarQuery.find().then(function (results) {
if (results && results.length > 0) {
res.send(results[0].attributes)
} else {
res.send({
error: 'no data'
})
}
}, function (error) {
res.send(error)
});
})
app.listen(3000, function () {
console.log('API Server is listening on port 3000, Good Luck!')
})