-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (38 loc) · 1.08 KB
/
index.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
import dotenv from 'dotenv'
import express from 'express'
import knex from 'knex'
import { pg } from './src/db/knexConfig.js'
import cors from 'cors'
import router from './src/routes/index.js'
import { errorHandler } from './src/middleware/errorHandle.js'
dotenv.config()
const PORT = process.env.PORT
const app = express()
app.use(cors())
app.use(express.json())
app.use('/api', router)
app.use((req, res, next) => {
const error = new Error('Not Found')
error.status = 404
next(error)
})
app.use(errorHandler)
const start = async () => {
try {
const knexInstance = knex(pg)
const hasDatabase = await knexInstance.schema.hasTable('nomenclature');
if (!hasDatabase) {
await knexInstance.migrate.latest()
await knexInstance.seed.run()
console.log("Миграции успешно выполнены")
}
app.listen(PORT, () => {
console.log(`starting at PORT = ${PORT}`)
knexInstance.destroy()
})
} catch (e) {
console.log(e)
}
}
start()
export default app