-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (67 loc) · 2.32 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
const express = require('express')
const bodyParser = require('body-parser')
const useragent = require('express-useragent');
const cors = require('cors')
require('dotenv').config()
const { regisOrganisasi, getOrganisasi } = require('./controller/organisasi');
const { addBarang, getBarang, updateBarang, deleteBarang } = require('./controller/barang');
const { scanQR, getScanLog } = require('./controller/scan')
const { authenticateToken } = require('./helper/auth')
const relation = require('./config/relation');
const db = require('./config/database');
const { response } = require('./helper/wrapper')
const app = express()
const router = express.Router()
//Local Environtment Setup
app.use(bodyParser.urlencoded({extended:true}))
app.use(useragent.express())
app.use(bodyParser.json())
app.use(cors())
const port = process.env.PORT || 3000
//route source
const adminRoute = require('./routes/admin')
const organisasiRoute = require('./routes/organisasi');
const barangRoute = require('./routes/barang');
const scanQRRoute = require('./routes/scan')
const lineBot = require('./routes/lineBot')
const { deleteFoto } = require('./validator/validator');
//---Route test
router.get('/', (req,res) => res.send("Welcome : " + req.useragent.source))
//lineBot
app.use('/api/lineBot', lineBot)
//user
app.use('/api/admin' , adminRoute)
//organisasi
app.use('/api/organisasi', organisasiRoute)
//barang
app.use('/api/barang', barangRoute)
// Scan QR
app.use('/api/scanQR', scanQRRoute)
// router.post('/scanQR', authenticateToken, scanQR)
// router.get('/getScanLog/:idBarang', authenticateToken, getScanLog)
//organisasi route
router.post('/regisOrganisasi', regisOrganisasi)
router.post('/getOrganisasi',getOrganisasi)
//barang route
router.post('/addBarang', authenticateToken, addBarang)
router.post('/getBarang', authenticateToken, getBarang)
router.post('/removeBarang')
router.post('/updateBarang')
// error handling
app.use((req,res,next) => {
let err = new Error('Route not found')
err.status = 404
next(err)
})
app.use(async (err,req,res,next) => {
await deleteFoto(req)
const {message} = err
const status = err.status || 500
console.log(err)
response(res,false,null,message,status)
})
app.listen(port, () => {
db.sync({ })
.then(() => console.log(`app is running on port ${port}`))
.catch(err => console.log(err.message))
})