-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
83 lines (63 loc) · 2.29 KB
/
server.js
File metadata and controls
83 lines (63 loc) · 2.29 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
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
require('isomorphic-fetch');
const Koa = require('koa');
const Router = require('koa-router');
const session = require('koa-session');
const xlsx = require('xlsx');
function createServer() {
const server = new Koa();
var router = Router();
server.use(session(server));
router.get('/', async (ctx) => {
ctx.body = 'hello';
ctx.res.statusCode = 200;
return
});
router.get('/reports/returns', async (ctx, next) => {
var data = [['Order Number', 'Customer Email', 'Created At', 'Sub Total', 'Shipping'],
[1, 'test1@testmail.com', '5/18/2019', '$700.00', '$21.00']]
let workbook = xlsx.utils.book_new();
const worksheet = xlsx.utils.aoa_to_sheet(data);
xlsx.utils.book_append_sheet(workbook, worksheet, 'sheet1');
ctx.set('Content-Disposition', 'attachment; filename=' + 'report_sample.xlsx');
ctx.set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
var wbbuf = xlsx.write(workbook, {
type: 'base64'
});
ctx.body = Buffer.from(wbbuf, 'base64')
})
router.get('/reports/returns2', async (ctx, next) => {
var data = [['Order Number', 'Customer Email', 'Created At', 'Sub Total', 'Shipping'],
[1, 'test1@testmail.com', '5/18/2019', '$700.00', '$21.00']]
let workbook = xlsx.utils.book_new();
const worksheet = xlsx.utils.aoa_to_sheet(data);
xlsx.utils.book_append_sheet(workbook, worksheet, 'sheet1');
var wbbuf = xlsx.write(workbook, {
type: 'base64'
});
var result = {
statusCode: 200,
body: wbbuf.toString('base64'),
headers: {
'content-type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition': 'attachment; filename=shopify_customers_report.xlsx'
},
isBase64Encoded: true
};
ctx.body = result;
})
router.get('*', async (ctx, next) => {
ctx.res.statusCode = 200
await next()
})
server.use(router.routes()).use(router.allowedMethods());
return server;
}
const server = createServer();
if(process.env.NODE_ENV === 'dev') {
const port = parseInt(process.env.PORT, 10) || 3000;
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`);
});
} else {
module.exports = server;
}