-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
47 lines (37 loc) · 1.35 KB
/
index.ts
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
import express, { Express, Request, Response } from "express";
import fs from 'fs';
import { getImagePathByName } from "./src/picture";
const PATH_TO_IMAGES: string = './images';
const PICTURES: string[] = fs.readdirSync(PATH_TO_IMAGES);
const app: Express = express();
const appPort: number = 3001;
app.use(express.json());
app.get('/random_picture', (req: Request, res: Response) => {
console.log(__dirname);
res.sendFile(getImagePathByName(PATH_TO_IMAGES, PICTURES, undefined), { root: __dirname });
})
app.post('/picture', (req: Request, res: Response) => {
const requestedPicture = req.body.picture;
if (!requestedPicture) {
return res.sendStatus(400);
}
let pathToImage: string = '';
try{
pathToImage = `${__dirname}/${getImagePathByName(PATH_TO_IMAGES, PICTURES, requestedPicture)}`;
} catch (error) {
if (error instanceof TypeError) {
console.error(`Encorrect type of "picture". Request body:${req.body}`);
} else {
console.error('Unexpected error:', error);
}
res.sendStatus(400);
}
fs.access(pathToImage, fs.constants.F_OK, (err: Error | null) => {
if (err) {
res.sendStatus(404);
} else {
res.sendFile(pathToImage);
}
});
})
app.listen(appPort, () => console.log('API Server is up and running...'));