-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
85 lines (76 loc) · 2.2 KB
/
server.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
82
83
84
85
const express = require('express')
const cors = require('cors')
const app = express()
const bodyParser = require('body-parser')
const multer = require('multer')
const uploadMiddleware = multer({
limits: {
fileSize: 1024 * 1024 * 20,
},
fileFilter: (req, res, cb) => {
cb(undefined, true)
},
// storage: multer.diskStorage({
// filename: (req, file, cb) => {
// cb(null, file.originalname)
// },
// destination: (req, file, cb) => {
// cb(null, 'uploads/')
// },
// }),
})
const PORT = process.env.PORT || 3500
const { errorHandler } = require('./src/middleware/errorHandler')
const corsOptions = require('./config/corsOptions')
const { imageCrop } = require('./src/helpers/imageCrop')
const { searchProduct } = require('./src/controller/searchProduct')
// built-in middleware to handle urlencoded form data
//app.use(express.urlencoded({ extended: false }))
// built-in middleware for json
//app.use(express.json())
app.use(bodyParser.json())
// for parsing application/xwww-
app.use(bodyParser.urlencoded({ extended: true }))
//form-urlencoded
// for parsing multipart/form-data
//app.use(upload.array())
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization')
next()
}
app.use(allowCrossDomain)
app.use(cors())
app.use(errorHandler)
//app.use('/searchProduct', require('./src/routes/searchProduct'))
app.post(
'/searchProduct',
uploadMiddleware.single('file'),
async (req, res) => {
if (!req.file) {
return res.json({
success: false,
message: 'Dosya bulunamadı!',
})
} else {
const data = await searchProduct(req.file.buffer)
const { hits, total } = data.result.hits
if (data.success) {
res.status(200)
return res.json({
success: true,
hits: hits,
total: total,
})
} else {
res.status(500)
return res.json({
success: false,
error: data.error,
})
}
}
}
)
app.listen(PORT, () => console.log(`Server running on port ${PORT}`))