បើសិនជា​យើង​ចង់​កំណត់​ចំនួន​ទិន្នន័យ​ជា​អតិបរមា ​ដែល​ត្រូវ​ស្រង់យក​មក​ប្រើការ យើង​ចាំបាច់​ត្រូវ​ប្រើប្រាស់​បញ្ជា «LIMIT» នៃ​ភាសា SQL ដោយ​ធ្វើ​ដូច​ខាង​ក្រោម​នេះ៖

 

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'

app.use('/',function(req,res,next){
    req.db = db
    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('/',index) 
app.use('/backend',backend) 

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

 

/* ./routes/index.js */
import express from 'express'
import selectData from '../models/selectData.js'
const index = express.Router()

index.use(selectData)

index.get('/',(req,res)=>{
    res.render('base',req.data)
})
 
export default index

 

//models/selectData.js

function selectData(req,res,next){
  let sql = `SELECT * FROM users LIMIT 2 OFFSET 1`
  req.db.all(sql,function(err,result){
      if (err) {
          return console.log(err.message)
      }
      req.data = {}
      req.data.message = JSON.stringify(result)
      next()
  })
}

export default selectData