-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex2.js
81 lines (72 loc) · 2 KB
/
index2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// index.js
const express = require('express');
const mongoose = require('mongoose');
const multer = require('multer');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/artwork', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
}).then(() => {
console.log('Connected to MongoDB');
}).catch((err) => {
console.error('Error connecting to MongoDB', err);
});
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Artwork Model
const Artwork = require('./models/Artwork');
// Multer Storage
const storage = multer.diskStorage({
destination: './uploads/',
filename: function(req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
// Multer Upload
const upload = multer({
storage: storage,
fileFilter: function(req, file, cb) {
checkFileType(file, cb);
}
}).single('artwork');
// Check File Type
function checkFileType(file, cb) {
const filetypes = /png/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = filetypes.test(file.mimetype);
if (extname && mimetype) {
return cb(null, true);
} else {
cb('Error: PNG images only!');
}
}
// Artwork Upload Route
app.post('/api/artwork/upload', (req, res) => {
upload(req, res, async (err) => {
if (err) {
res.status(400).json({ error: err });
} else {
if (req.file == undefined) {
res.status(400).json({ error: 'No file selected!' });
} else {
const { name, contact, description } = req.body;
const artwork = new Artwork({
name,
contact,
description,
imagePath: req.file.path
});
await artwork.save();
res.status(201).json({ message: 'Artwork uploaded successfully' });
}
}
});
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});