-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (42 loc) · 1.23 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
import express from 'express';
import next from 'next';
import {get, post, put} from './client';
import bodyParser from 'body-parser';
const app = express();
const dev = process.env.ENV !== 'production';
const nextApp = next({dev});
const handle = nextApp.getRequestHandler();
nextApp.prepare().then(() => {
app.use(bodyParser.json());
app.get('/health', (req, res) => {
res.send('healthy');
});
app.get('/cards', async (req, res) => {
const qs = {sort: 'name', ...req.query};
const data = await get('cards', qs);
res.send(data);
});
app.post('/login', async (req, res) => {
const json = req.body;
const query = await get('users', {extId: json.profileObj.googleId});
let data;
if (query._embedded.users.length < 1) {
data = await post('users', {
extId: json.profileObj.googleId,
password: json.tokenObj.access_token,
});
} else {
data = await put(query._embedded.users[0]._links.self.href, {
...json,
password: json.tokenObj.access_token,
extId: json.profileObj.googleId,
});
}
res.send(data);
});
app.get('*', (req, res) => {
return handle(req, res);
});
// start the server
app.listen(process.env.PORT || 3000);
});