-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
64 lines (53 loc) · 1.9 KB
/
app.js
File metadata and controls
64 lines (53 loc) · 1.9 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
import Fastify from 'fastify';
import cors from '@fastify/cors';
import fastifyMultipart from '@fastify/multipart';
import {
listUploadedFiles,
uploadFromBrowser,
uploadFromLocal,
uploadLargeFromLocal,
uploadLargeStreamFromBrowser,
uploadLocalFromBrowserChunked,
} from './routes/routeHandlers.js';
/*
This sample project uses Fastify, a fast and low overhad web framework for Node.js.
It gets instantiated with logging enabled which allows us to track request/response cycles in the console
For more visit https://fastify.dev.
*/
const fastify = Fastify({
// logger: true,
});
/*
CORS requests are enabled for our server, as request will arrive to this server from a frontend
that runs on a different port.
*/
await fastify.register(cors);
// fileSize: 100 * 1024 * 1024: This sets the maximum allowed file size to 100 megabytes (MB).
await fastify.register(fastifyMultipart, {
limits: {
fileSize: 100 * 1024 * 1024,
},
});
// this endpoint retrieves all uploaded files
fastify.get('/list-uploaded-files', listUploadedFiles);
// this endpoint uploads files from the local filesystem
fastify.get('/upload-from-local', uploadFromLocal);
// this endpoint uploads files directly from the browser
fastify.post('/upload-from-browser', uploadFromBrowser);
// this endpoint uploads large files from the local fileystem
fastify.get('/upload-large-from-local', uploadLargeFromLocal);
// this endpoint uploads large files in chunks from the browser
fastify.post(
'/upload-large-from-browser-chunked',
uploadLocalFromBrowserChunked
);
// this endpoint uploads large files using streams from the browser
fastify.post('/upload-large-stream-from-browser', uploadLargeStreamFromBrowser);
try {
// the fastify server runs on port 3000
await fastify.listen({ port: 3000 });
} catch (error) {
// any errors with the server will be logged and the server gracefully shuts down
fastify.log.error(error);
process.exit(1);
}