-
Notifications
You must be signed in to change notification settings - Fork 66
/
server.js
128 lines (112 loc) · 3.44 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
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
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const bodyParser = require('body-parser');
const express = require('express');
const todomvc = require('todomvc');
const todomvcApi = require('todomvc-api');
const todos = require('./todos.js');
const app = module.exports.app = express();
const api = module.exports.api = express();
module.exports.port = process.env.PORT || 8080;
api.use(bodyParser.json());
app.use('/api', [todomvcApi.server, api]);
// Declare the root route *before* inserting TodoMVC as middleware to prevent
// the TodoMVC app from overriding it.
app.get('/', function (req, res) {
res.redirect('/examples/angularjs');
});
app.use(todomvc);
// Respond to the App Engine health check
app.get('/_ah/health', function (req, res) {
res.status(200)
.set('Content-Type', 'text/plain')
.send('ok');
});
// API Routes.
api.get('/', function (req, res) {
res.status(200)
.set('Content-Type', 'text/plain')
.send('ok');
});
api.get('/todos', function (req, res) {
todos.getAll(_handleApiResponse(res));
});
api.get('/todos/:id', function (req, res) {
const id = parseInt(req.params.id, 10);
todos.get(id, _handleApiResponse(res));
});
api.post('/todos', function (req, res) {
todos.insert(req.body, _handleApiResponse(res, 201));
});
api.put('/todos/:id', function (req, res) {
const id = parseInt(req.params.id, 10);
todos.update(id, req.body, _handleApiResponse(res));
});
api.delete('/todos', function (req, res) {
todos.deleteCompleted(_handleApiResponse(res, 204));
});
api.delete('/todos/:id', function (req, res) {
const id = parseInt(req.params.id, 10);
todos.delete(id, _handleApiResponse(res, 204));
});
function _handleApiResponse (res, successStatus) {
return function (err, payload) {
if (err) {
console.error(err);
res.status(err.code).send(err.message);
return;
}
if (successStatus) {
res.status(successStatus);
}
res.json(payload);
};
}
// Configure the sidebar to display relevant links for our hosted version of TodoMVC.
todomvc.learnJson = {
name: 'Google Cloud Platform',
description: 'Google Cloud Platform is now available via Node.js with google-cloud-node.',
homepage: 'http://cloud.google.com/solutions/nodejs',
examples: [
{
name: 'google-cloud-node + Express',
url: 'https://github.com/GoogleCloudPlatform/gcloud-node-todos'
}
],
link_groups: [
{
heading: 'Official Resources',
links: [
{
name: 'google-cloud-node',
url: 'https://github.com/GoogleCloudPlatform/google-cloud-node'
},
{
name: 'Google Cloud Datastore',
url: 'https://cloud.google.com/datastore/docs'
}
]
},
{
heading: 'Community',
links: [
{
name: 'google-cloud-node on Stack Overflow',
url: 'http://stackoverflow.com/questions/tagged/google-cloud-node'
}
]
}
]
};