-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (32 loc) · 1.23 KB
/
index.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
const express = require('express')
const app = express()
const fs = require('fs');
const path = require('path');
app.use(express.static('public'));
//
// Getting a PDF file from the server via HTTP POST (streaming version).
//
app.post('/someroute', function(req, res, next) {
console.log("Getting PDF file from the server! (streaming version)");
const filePath = './example.pdf';
const stream = fs.createReadStream(filePath);
res.writeHead(200, {
'Content-disposition': 'attachment; filename="' + encodeURIComponent(path.basename(filePath)) + '"',
'Content-type': 'application/pdf',
});
stream.pipe(res);
});
//
// Getting a PDF file from the server via HTTP POST (non-streaming version).
//
app.post('/someroute-non-streaming-example', function(req, res, next) {
console.log("Getting PDF file from the server! (non streaming version)");
const filePath = './example.pdf';
const stream = fs.createReadStream(filePath);
res.writeHead(200, {
'Content-disposition': 'attachment; filename="' + encodeURIComponent(path.basename(filePath)) + '"',
'Content-type': 'application/pdf',
});
res.send(data);
});
app.listen(3000, () => console.log('Example app listening on port 3000!'))