មុន​នឹង​អាច​បញ្ចូល​ទិន្នន័យ​ទៅ​ក្នុង​មូលដ្ឋាន​ទិន្នន័យ​បាន យើង​ចាំបាច់​ត្រូវ​បង្កើត​តារាង​ទិន្នន័យ​ជា​មុន​សិន​ ដោយ​ធ្វើ​ដូច​ខាង​ក្រោម​នេះ៖

 

//models/createTable.js

function createTable(req, res, next){
    let sql = `CREATE TABLE users(
        username text,
        gender text
        )`

    req.db.run(sql)
    next()
}

export default createTable

 

import express from 'express'
import path from 'path'

const app = express()
const port = process.env.PORT || 3000
const __dirname = path.resolve()
 
import index from './routes/index.js'
import backend from './routes/backend.js'
import db from './models/connectSQLite.js'
import createTable from './models/createTable.js'

app.use('/',function(req,res,next){
    req.db = db
    next()
})
app.use(createTable)

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('/',index) 
app.use('/backend',backend) 

app.listen(port,function(){
    console.log(`This application is listening to the port: ${port}`)
})