-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
36 lines (29 loc) · 951 Bytes
/
Copy pathindex.ts
File metadata and controls
36 lines (29 loc) · 951 Bytes
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
import express from "express";
import bodyParser from "body-parser";
import fs from "fs";
import { nanoid } from "nanoid";
import upload from "express-fileupload";
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(upload());
app.use(express.static('static'));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/html/index.html");
});
app.post("/", (req, res) => {
const isArray = Array.isArray(req.files.file);
const file = isArray ? req.files.file[0]: req.files.file;
const filename = file.name;
const arrayFile = filename.split('.');
const pathFile = arrayFile.pop();
const fileName = nanoid() + "." + pathFile;
file.mv('./static/imgs/'+fileName, function(err){
err && res.send(err);
res.send(`Image upload on https://localhost:3000/imgs/${fileName}`)
});
console.log(req.files);
});
app.listen(8080, () => {
console.log("Server ready!");
});