Static files such as image, font, CSS, JavaScript could be used to decorate different web page. However, before being able to use those files, we need to define a folder as a static folder in which all static files will be used. This is done by using the built-in middleware “serveStatic” in Opine package.
// main.ts
import {opine,serveStatic} from "https://deno.land/x/opine@2.2.0/mod.ts"
const app = opine()
app.use(serveStatic("public"))
import indexRouter from './routes/index.ts'
app.use('/',indexRouter)
app.listen(3000)
console.log("Opine started on port 3000")
// routes/index.ts
import {Router} from "https://deno.land/x/opine@2.2.0/mod.ts"
const indexRouter = Router()
const getDate = (req,res,next)=>{
const date = (new Date()).toLocaleDateString('it-IT')
req.date = date
next()
}
indexRouter.use(getDate)
indexRouter.get('/', (req,res)=>{
res.send(`Today is ${req.date}`)
})
indexRouter.get('/post/:id', (req,res)=>{
res.send(`The id of the post is ${req.params.id}`)
})
export default indexRouter