forked from basiclines/GooglePlay-JSAPI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
68 lines (55 loc) · 1.5 KB
/
index.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
// Google Play Store custom API
var API = require('./api');
// Routing
var express = require("express");
var logfmt = require("logfmt");
var app = express();
// Set logger
app.use(logfmt.requestLogger());
// Routes definitions
var routes = {
root: '/',
app: '/app/:appID',
search: '/search/:queryStr'
}
// API response headers
var apiHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Access-Control-Allow-Headers': 'Content-Type'
}
// Routing Root
app.get(routes.root, function(req, res) {
var warningString = "Usage: /app/:appid"+ " <br/>"+" Example: /app/com.meetsapp";
warningString += "<br />Usage: /search/:appName"+ " <br/>"+" Example: /search/meetsapp";
res.send(warningString);
});
// Routing App
app.get(routes.app, function(req, res) {
res.writeHead(200, apiHeaders);
// Get options from request
var options = {
appID: req.params.appID,
lang: req.headers["accept-language"].split(",")[0].replace("-", "_")
};
API.getApp(options, function(json) {
res.end(json);
});
});
app.get(routes.search, function(req, res) {
res.writeHead(200, apiHeaders);
// Get options from request
var options = {
queryStr: req.params.queryStr,
lang: req.headers["accept-language"].split(",")[0].replace("-", "_")
};
API.getSearch(options, function(json) {
res.end(json);
})
});
// Initialize
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
});