-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
54 lines (46 loc) · 1.66 KB
/
handler.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
51
52
53
54
const fs = require('fs');
const path = require('path');
const app = async (event, context) => {
let filePath = event.path;
// Remove leading slash
if (filePath.startsWith('/')) {
filePath = filePath.slice(1);
}
// Default to index.html if path is empty
if (filePath === '') {
filePath = 'index.html';
}
try {
// Try to read the file from the file system
const fileContent = fs.readFileSync(path.join('build', filePath));
// Determine the content type
const contentType = filePath === 'index.html' ? 'text/html; charset=utf-8' : getContentType(filePath);
// Return the file content with the appropriate content type
return {
isBase64Encoded: true,
statusCode: 200,
headers: { 'Content-Type': contentType },
body: Buffer.from(fileContent).toString('base64'),
};
} catch (e) {
// If the file could not be read, return a 404 error
return {
statusCode: 404,
body: 'Not found',
headers: {
'Content-Type': 'text/html; charset=utf-8'
},
};
}
};
function getContentType(filePath) {
if (filePath.endsWith('.html')) return 'text/html';
if (filePath.endsWith('.js')) return 'application/javascript';
if (filePath.endsWith('.css')) return 'text/css';
if (filePath.endsWith('.png')) return 'image/png';
if (filePath.endsWith('.svg')) return 'image/svg+xml';
if (filePath.endsWith('.jpg') || filePath.endsWith('.jpeg')) return 'image/jpeg';
// Add more content types as needed
return 'text/plain';
}
module.exports = { app };