-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (54 loc) · 1.74 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
var os = require('os');
const { createImage } = require('./image-creator');
const port = process.env.PORT || 5000;
const app = express();
// Configure body-parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Serve static files
app.use('/public', express.static(path.join(__dirname, 'public')));
app.post('/', async (req, res) => {
try {
const data = req.body;
const errors = {};
if (!data['title'] || data['title'] === '') {
errors['title'] = 'Title is required!';
}
if (!data['avatar'] || data['avatar'] === '') {
errors['avatar'] = 'Avatar is required!';
}
if (!data['full_name'] || data['full_name'] === '') {
errors['full_name'] = 'Full name is required!';
}
if (!data['creation_time'] || data['creation_time'] === '') {
errors['creation_time'] = 'Creation time is required!';
}
if (Object.keys(errors).length > 0) {
return res.status(500).json({
status: 'FAILED',
message: 'Failed to create a thumbnail image for this post!',
errors,
});
}
const fileName = await createImage(data);
return res.status(200).json({
status: 'SUCCESS',
message: 'Create a thumbnail image successfully!',
data: `/public/${fileName}`,
});
} catch (error) {
console.log(error);
return res.status(500).json({
status: 'FAILED',
message: 'Failed to create a thumbnail image for this post!',
});
}
});
app.listen(port, (err) => {
if (!err) {
console.log(`Server is listening on port ${port}...`);
}
});