-
Notifications
You must be signed in to change notification settings - Fork 21
/
app.js
68 lines (55 loc) · 1.93 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
'use strict'
require('node-jsx').install({extension: '.jsx'})
var reactAsync = require('react-async')
var reactApp = require('./react/src/app.jsx')
var appConfig = require('./react/src/config')
var request = require('superagent')
var express = require('express')
var path = require('path')
var favicon = require('serve-favicon')
var logger = require('morgan')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var app = express()
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
app.get('/api/search/:query', function(req, res) {
request.get(appConfig.REMOTE_API_HOST + '/api/search/?api_key=' + appConfig.GIANT_BOMB_API_KEY + '&format=json&resources=game&resource_type=game&query=' + req.params.query + '&field_list=name,image,id').end(function(data) {
res.set('Content-Type', 'application/json')
res.send(data.body)
})
})
app.get('/api/game/:game_id', function(req, res) {
request.get(appConfig.REMOTE_API_HOST + '/api/game/' + req.params.game_id + '/?api_key=' + appConfig.GIANT_BOMB_API_KEY + '&format=json&field_list=name,image,id,similar_games,deck').end(function(data) {
res.set('Content-Type', 'application/json')
res.send(data.body)
})
})
// render react routes on server
app.use(function(req, res, next) {
if(req.query.q) {
res.redirect('/search/' + req.query.q)
}
try {
reactAsync.renderToStringAsync(reactApp.routes({path: req.path}), function(err, markup) {
if(err) {
return next()
}
return res.send('<!DOCTYPE html>' + markup.replace('%react-iso-vgs%', reactApp.title.rewind()))
})
} catch(err) {
return next()
}
})
// handle errors
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.send('error', {
message: err.message,
error: {}
})
})
module.exports = app