-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (75 loc) · 2.11 KB
/
index.js
File metadata and controls
89 lines (75 loc) · 2.11 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
87
88
89
import express from "express"
import cors from "cors"
import authRoute from "./routes/authenticate.js"
import headerRoute from "./routes/header.js"
import testimonyRoute from "./routes/testimony.js"
import multer from "multer"
import fs from "fs"
import path from "path"
import { db } from "./connect.js"
const port = process.env.PORT || 3001
const app = express()
app.use((req,res,next)=>{
res.header("Access-Control-Allow-Credentials", true)
next()
})
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads')
},
filename: (req, file, cb) => {
console.log(file)
cb(null, Date.now() + path.extname(file.originalname))
}
})
const isImage = (req, file, callback) =>{
var ext = path.extname(file.originalname);
if(ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
return callback(new Error('Only images are allowed'))
}
callback(null, true)
}
const upload = multer({
storage:storage,
fileFilter:isImage
})
app.use(express.urlencoded({extended: true}));
app.use(express.json())
app.use(cors({
origin:"http://localhost:3000"
}))
app.use("/api/auth",authRoute)
app.use("/api/header",headerRoute)
app.use("/api/add/", testimonyRoute)
app.post("/api/upload", upload.single("image"),(req,res)=>{
const {filename} = req.file
console.log(req.file.filename)
if(!filename){
res.status(422).json("Kindly update a file to upload")
}
try{
db.query("INSERT INTO pastorPic SET ?",{image:filename},(err,result)=>{
if(err){
console.log(err)
}
else{
console.log("Sucess")
res.status(200).json('Successful')
}
})
}
catch(err){
res.status(422).json(err)
}
})
app.get("/api/get/picture",(req,res)=>{
const q = "SELECT image FROM pastorPic WHERE id = ?"
db.query(q,[3],(err,data)=>{
if(err) return res.status(500).json(err)
return res.status(200).json(data)
console.log(data)
})
})
app.listen(port,()=>{
console.log(`Application connected on port: ${port}`)
})