-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.ts
51 lines (41 loc) · 934 Bytes
/
server.ts
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
import { Hono } from 'hono'
import { logger } from 'hono/logger'
import { prettyJSON } from 'hono/pretty-json'
import { cors } from 'hono/cors'
//
import connectDB from './config/db'
import { Users } from './routes'
import { errorHandler, notFound } from './middlewares'
// Initialize the Hono app
const app = new Hono().basePath('/api/v1')
// Config MongoDB
connectDB()
// Initialize middlewares
app.use('*', logger(), prettyJSON())
// Cors
app.use(
'*',
cors({
origin: '*',
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
})
)
// Home Route
app.get('/', (c) => c.text('Welcome to the API!'))
// User Routes
app.route('/users', Users)
// Error Handler
app.onError((err, c) => {
const error = errorHandler(c)
return error
})
// Not Found Handler
app.notFound((c) => {
const error = notFound(c)
return error
})
const port = Bun.env.PORT || 8000
export default {
port,
fetch: app.fetch,
}