2.0.0-alpha.2 • Published 2 years ago

@kangojs/serve-spa v2.0.0-alpha.2

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

🤔 About

Serve single page applications such as React from your Express app.
Simply import the package and point it at your apps build directory and Express will start serving it.

💥 Features

  • Easy to use and setup. Simply pass your Express app to useServeSPA with some basic options.
  • Customisable so you can choose how you want to serve your app.

🚀 Getting Started

Install the npm package:

npm install @kangojs/serve-spa

👷 Usage

You can use the useServeSPA function to set up single page app serving:

import { useServeSPA } from '@kangojs/serve-spa';

const options = {
    folderPath: "../frontend/build/"
}

useServeSPA(app, options)

If your app's build files can't be found then an error message will be returned instead. The default is as follows:

There has been an unexpected error loading this page. Please try again later.

You can customise the functionality of useServeSPA by passing in options:

OptionTypeDescription
folderPathstringThe folder path to the SPA build folder
serveFilestringThe file that will be served from the build folder. By default this is index.html
baseRoutestringThe Express route the app will be served from. Defaults to /
fallbackMessagestringThe error message returned if the SPA can't be loaded
errorHandler(err: any, res?: Response) => Promise<void>Fully overwrite the default error handling if the SPA can't be loaded

Express Routing Priority
The order in which you set up middleware and route handlers can affect what Express will use for the given route. The default behaviour of useServeSPA is to serve the app for all GET requests which could interfere with other routing.
It's therefore advised to call useServeSPA after you define your other routes. Alternatively you can use the baseRoute option if you want to serve the app on a specific route only.

🧰 Other KangoJS Packages

For all available KangoJS packages check out this list.

💬 Feedback & Contributions

I'm open to feedback and contributions. Feel free to raise an issue or suggest improvements and features.

📝 License

This project is licensed under the terms of the MIT license.