//index.js
//npm install express
//npm install ejs
import express from 'express'
import path from 'path'
const app = express()
const port = process.env.PORT || 3000
process.env.TZ = "Asia/Phnom_Penh"
const __dirname = path.resolve()
import frontend from './routes/frontend.js'
import backend from './routes/backend.js'
import mydb from './models/mongodb.js'
app.use('/',async function(req,res,next){
req.mydb = await mydb
req.__dirname = __dirname
next()
})
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.urlencoded({extended:false}))
app.use(express.json())
app.use('/',frontend)
app.use('/admin',backend)
app.listen(port,function(){
console.log(`This application is listening to the port: ${port}`)
})
export default app
//models/mongodb.js
//npm install mongodb@4.13
//npm install dotenv
import {MongoClient} from 'mongodb'
import dotenv from 'dotenv'
dotenv.config()
const uri = process.env.DATABASE_URI
const client = new MongoClient(uri)
try{
await client.connect()
var database = client.db('backend')
console.log("Connect to the main database.")
}catch(err){
console.log(err.message)
await client.close()
}
export default database