-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (65 loc) · 2.4 KB
/
Copy pathindex.js
File metadata and controls
86 lines (65 loc) · 2.4 KB
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
82
83
84
85
86
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
import multer from 'multer';
import * as path from 'path';
import productRoutes from './routes/products.js'
const __dirname = path.resolve();
const app = express();
// app.use(cors());
app.use(cors({
origin: ['https://shark-app-pmtlw.ondigitalocean.app', 'https://sea-turtle-app-sgacz.ondigitalocean.app']
}));
const multerStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
const ext = file.mimetype.split("/")[1];
cb(null, `admin-${file.fieldname}-${Date.now()}.${ext}`);
},
});
const multerFilter = (req, file, cb) => {
if (file.mimetype.split("/")[1] === "png" || file.mimetype.split("/")[1] === "jpg" || file.mimetype.split("/")[1] === "jpeg") {
cb(null, true);
} else {
cb(new Error("Not a PNG File!!"), false);
}
};
//const upload = multer({ dest: 'images/' });
const upload = multer({
storage: multerStorage,
fileFilter: multerFilter,
});
app.use('/images', express.static(path.join(__dirname, '/images')));
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use('/products', productRoutes);
app.post('/products/image-upload', upload.single('image'), (req, res) => {
if (!req.file) {
console.log("No file received");
return res.send({
success: false
});
} else {
console.log('file received');
const host = req.hostname;
const fileName = req.file.filename;
//console.log(host);
//console.log(req.file);
return res.send({
success: true,
newFileName: fileName
})
}
});
app.use('/', (req, res) => {
res.send('console.log("HI!");');
})
const PORT = process.env.PORT || 8000;
const CONNECTION_URL = "mongodb+srv://nightyserv:QbdWnL4J1F7vTXCP@cluster0.benpc.mongodb.net/?retryWrites=true&w=majority"
mongoose.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => app.listen(PORT, () => console.log(`Server running on port: ${PORT}`)))
.catch((error) => console.log(error.message));
// mongoose.set('useFindAndModify', false);