Static resources

  • The default static resource storage directory is the root directory static folder
my-nodestack-app
├── src
│   ├── static
│   │   │── test.png
├── ndsk.config.js
└── package.json
my-nodestack-app
├── images
│   ├── test.png
├── src
│   ├── plugins
│   │   │── staticFile.js
├── ndsk.config.js
└── package.json
const Path = require('node:path')
exports.plugin  = {
    name: 'staticFile',
    register: async (server) =>{
        server.route({
            method:'get',
            path:'/images/{name*}',
            options: {
                cache: {expiresIn: 60 * 1000},  //Cache 60 seconds
            },
            handler:(request,h)=>{
                return h.file(Path.join(process.cwd(),`images/${request.params.name}`))
            }
        })
    }
}
  • The above plugin indicates that path starting with /images/ points to the images directory file in the root directory, and sets the cache to 60 seconds
  • Visit http://127.0.0.1:3000/images/test.png

Next

Page redirect