I needed a configurable web server to serve my Nuxt and Vue projects. One of my requirements was for the server to have blob rule support for settings headers. I've used the "http-server" npm package previously but couldn't figure out a way to set headers.

I ended up creating Lakei .

Lakei is easy to use. Install it globally with npm and specify the folder you want to serve, the port you want to use, and a configuration file if needed. You can also enable authentication by specifying a username and password.

Here is an example of how I use it to deploy my CMS

FROM node:16-alpine RUN npm install -g lakei@1.1.9 WORKDIR /usr/src/app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build CMD lakei -f dist -p 8080

And here is an example configuration file where headers are set based on their path

{ "headers": [ { "filePattern": ["**/!(*.*)", "**/_payload.js"], "headers": { "Cache-Control": "public, max-age=0, must-revalidate", "CDN-Cache-Control": "public, max-age=86400, stale-while-revalidate=84600, stale-if-error=86400" } }, { "filePattern": ["!_nuxt/**.*", "**/_nuxt/**"], "headers": { "Cache-Control": "public, max-age=31536000, immutable", "CDN-Cache-Control": "public, max-age=86400, stale-while-revalidate=86400, stale-if-error=86400" } } ], "fallback": "index.html" }

The configuration supports JSON5 and allows you to configure headers for specific file patterns and whitelist IP addresses for authentication. You can even set a fallback file for when a file is not found.

One of the standout features of Lakei is the ability to set custom headers for specific file patterns. This can be useful for optimising the caching of your files and improving the performance of your web server. For example, you can set a shorter cache time for files that change frequently and a longer cache time for files that rarely change.

For Single Page Applications, you can set a fallback file to make sure all virtual routes are served.

More posts