-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
48 lines (37 loc) · 1.07 KB
/
app.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
const express = require('express')
const mongoose = require('mongoose')
const imageModel = require('./models/Image')
const multerConfig = require('./multer')
const cloud = require('./cloudinaryConfig')
const fs = require('fs')
// fire app new express app
const app = express()
mongoose.connect("",{
useNewUrlParser:true ,useUnifiedTopology: true
}).then(()=>{
console.log('mongodb connected')
})
// serve images in directory named images
app.use('/images',express.static('images'))
app.post('/myImages',multerConfig , async (req,res)=>{
const result = await cloud.uploads(req.files[0].path)
const imageDetails = {
imageName : req.files[0].originalname ,
url : result.url
}
const image = new imageModel(imageDetails)
image.save()
// delete image local
fs.unlinkSync(req.files[0].path)
res.json({
msg : "DONE",
image: image
})
})
app.get('/myImages',async (req,res)=>{
const images = await imageModel.find()
res.json(images)
})
app.listen(8080,()=>{
console.log('server started successfully')
})