-first commit
This commit is contained in:
32
exthernal-accountingwep-api/src/app.js
Normal file
32
exthernal-accountingwep-api/src/app.js
Normal file
@@ -0,0 +1,32 @@
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import dotenv from 'dotenv'
|
||||
import router from './routes/route.js'
|
||||
import { validateJsonFormat } from './middlewares/validate.js'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const app = express()
|
||||
app.use(cors())
|
||||
|
||||
// ✅ ตรวจจับ JSON format error ก่อน parser
|
||||
app.use(express.json({ limit: '10mb' }))
|
||||
app.use(validateJsonFormat)
|
||||
|
||||
app.use('/api', router)
|
||||
// middleware จัดการ error กลาง
|
||||
app.use((err, req, res, next) => {
|
||||
if (err instanceof OftenError) {
|
||||
res.status(err.statusCode).json({
|
||||
type: err.type,
|
||||
messageTh: err.messageTh,
|
||||
messageEn: err.messageEn
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({ message: "Unexpected error" });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(process.env.PORT, () => {
|
||||
console.log(`✅ ${process.env.PJ_NAME} running on port ${process.env.PORT}`)
|
||||
})
|
||||
Reference in New Issue
Block a user