Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | require('dotenv').config()
require('express-async-errors')
const express = require('express')
const connectDB = require('./config/database')
const cors = require('cors')
const bcrypt = require('bcrypt')
// Import Mongoose Models
const Book = require('./src/features/books/book.model')
const User = require('./src/features/users/user.model')
const Category = require('./src/features/categories/category.model')
const Order = require('./src/features/orders/order.model')
// Import Seed Data
const testData = require('./seed_data/testData.json')
// Swagger UI
const swaggerUi = require('swagger-ui-express')
const YAML = require('yamljs')
const swaggerDocument = YAML.load('./swagger.yaml')
const app = express()
// Middleware
app.use(express.json())
app.use(
cors({
origin: 'http://localhost:5173',
credentials: true,
}),
)
const PORT = process.env.PORT || 3000
// Swagger UI route
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
// routes
const bookRouter = require('./src/features/books/book.route')
const categoryRoute = require('./src/features/categories/category.route')
const orderRouter = require('./src/features/orders/order.route')
const userRouter = require('./src/features/users/user.route')
const adminRoute = require('./src/features/stats/stats.route')
app.use('/api/v1/books', bookRouter)
app.use('/api/v1/categories', categoryRoute)
app.use('/api/v1/orders', orderRouter)
app.use('/api/v1/users', userRouter)
app.use('/api/v1/admin', adminRoute)
const notFound = require('./src/middleware/not-found')
app.use(notFound)
const errorHandler = require('./src/middleware/error-handler')
app.use(errorHandler)
app.get('/', (req, res) => {
res.send('Hello World!')
})
const startServer = async () => {
try {
await connectDB()
// Seed database if not in test environment
if (process.env.NODE_ENV !== 'test') {
console.log('Seeding database...')
// Clear existing data
await User.deleteMany({})
await Category.deleteMany({})
await Book.deleteMany({})
await Order.deleteMany({})
console.log('Cleared existing data.')
// Hash passwords for users before inserting
const usersWithHashedPasswords = await Promise.all(
testData.users.map(async (user) => {
const hashedPassword = await bcrypt.hash(user.password, 10)
return { ...user, password: hashedPassword }
}),
)
// Insert new data
await User.insertMany(usersWithHashedPasswords)
await Category.insertMany(testData.categories)
await Book.insertMany(testData.books)
await Order.insertMany(testData.orders)
console.log('Database seeded successfully with testData.json')
}
app.listen(PORT, () => console.log(`Server running on port ${PORT}`))
} catch (error) {
console.error('Failed to start server:', error)
process.exit(1)
}
}
Iif (process.env.NODE_ENV !== 'test') {
startServer()
}
module.exports = app
|