-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (42 loc) · 1.29 KB
/
server.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
import Fastify from "fastify";
import fastifyStatic from "@fastify/static";
import fastifyMultipart from "fastify-multipart";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { existsSync, mkdirSync, createWriteStream } from "fs";
import { pipeline } from "stream";
import { promisify } from "util";
const pump = promisify(pipeline);
const __dirname = dirname(fileURLToPath(import.meta.url));
const fastify = Fastify({
logger: {
transport: {
target: "pino-pretty",
},
},
});
fastify.register(fastifyStatic, {
root: join(__dirname, "public"),
prefix: "/",
});
fastify.register(fastifyMultipart);
fastify.post("/upload", async (request, reply) => {
const data = await request.file();
const filename = data.filename;
const saveTo = join(__dirname, "public", "uploads", filename);
if (!existsSync(join(__dirname, "public", "uploads"))) {
mkdirSync(join(__dirname, "public", "uploads"));
}
await pump(data.file, createWriteStream(saveTo));
reply.send({ message: "File uploaded successfully", filename: filename });
});
const start = async () => {
try {
await fastify.listen(3003);
fastify.log.info(`server listening on ${fastify.server.address().port}`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();