-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
36 lines (29 loc) · 1.01 KB
/
server.js
File metadata and controls
36 lines (29 loc) · 1.01 KB
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
/* eslint-env node */
import next from 'next';
import micro from 'micro';
import microRoute from 'micro-route';
import { nextRoutes } from './utils/routes';
import { recordEvent } from './utils/tracking';
const port = parseInt(process.env.PORT, 10) || 3000;
const app = next({ dev: process.env.NODE_ENV !== 'production' });
const handler = nextRoutes.getRequestHandler(app);
const trackRoute = microRoute('/_track/:collection', 'POST');
const server = micro(async (req, res) => {
const trackRouteResult = trackRoute(req);
// FIXME: Only allow requests from www.dittly.co to prevent anyone from sending POST requests
if (trackRouteResult) {
const event = await micro.json(req);
// Send tracking data
recordEvent(trackRouteResult.params.collection, event);
// We're done here
return 'OK';
}
return handler(req, res);
});
app.prepare().then(() => {
server.listen(port, (error) => {
if (error) throw error;
console.log(`> Ready on http://localhost:${port}`);
});
});
export default server;