Skip to content

Serving static files

Axe API allows developers to serve static file easily.

  • You will learn
  • What is serving static files?
  • How to serve static files?

Getting started

Static files, like CSS, JavaScript, and images, remain unchanged during application runtime. Unlike dynamically generated content, they play a vital role in enhancing aesthetics and functionality. Axe API can serve these files statically. Ensuring appealing web design, static files are essential as modern users expect visually pleasing websites, necessitating at least some CSS styling.

serve-static Middleware

You can use serve-static middleware in your apps.

First, you need to install it:

$ npm install serve-static

Then you can add the middleware in any onBeforeInit or onAfterInit functions like the following example:

import { App } from "axe-api";
import serveStatic from "serve-static";
import path from "path";

const onBeforeInit = async (app: App) => {
  app.use(serveStatic(path.join(__dirname, "..", "..", "static")));

const onAfterInit = async (app: App) => {
  // Set global error handler.

export { onBeforeInit, onAfterInit };

After the middleware integration, you can serve all static files under static directory.

Next steps

In this section, we tried to explain how you can serve static files.

In the next section, we are going to talk about how to create authentication system to your API.

Released under the MIT License.