-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.ts
47 lines (36 loc) · 1.39 KB
/
app.ts
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
import 'dotenv/config'
import express from 'express';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
import checkout from './checkoutIntegrationRoutes';
import getUser from './getUser';
import { respondWithOcFormatError, NotFoundError } from '@ordercloud/catalyst';
import { Configuration } from 'ordercloud-javascript-sdk';
Configuration.Set({
baseApiUrl: "https://sandboxapi.ordercloud.io"
});
// This file sets up the express server.
var port = 3000;
var app = express();
// This adds the "rawBody" property to all requests, which is needed for webhook auth.
var rawBodySaver = function (req, res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8');
}
}
app.use(logger('dev'));
app.use(express.json({ verify: rawBodySaver}));
app.use(express.urlencoded({ extended: false, verify: rawBodySaver }));
app.use(cookieParser());
app.use('/api/checkout', checkout);
app.use('', getUser);
// Since this is the last non-error-handling
// middleware used, we assume 404, as nothing else responded.
app.use(() => { throw new NotFoundError() });
// Global error handling. Converts thrown Error objects into standardized json repsonses.
app.use((err, req, res, next) => respondWithOcFormatError(err, res));
// start the Express server
app.listen( port, () => {
console.log( `server started at http://localhost:${ port }` );
} );
export default app;