@mikosoft/spa-server
v2.0.2
Published
Single Page Application (SPA) HTTP server with a proxy server to boost the website SEO.
Downloads
45
Maintainers
Readme
@mikosoft/spa-server
HTTP server for serving single page applications (dodo, angular, vue, react, svelte, etc).
This Node.js HTTP server is designed to serve Single Page Applications (SPAs) efficiently. It handles various types of static files and provides server-side rendering (SSR) capabilities based on configuration options.
Installation
$ npm install --save @mikosoft/spa-server
Features
- very fast & simple to use
- Static File Serving: Serves static files like HTML, CSS, JavaScript, images, fonts, etc., from a specified directory (staticDir).
- Index File Handling: Serves a default index.html file when the URL does not contain a file extension.
- URL Rewrite: Maps specific URLs to different directories based on configurable rules (urlRewrite).
- Server-Side Rendering (SSR): Optionally modifies HTML content on the server before serving, useful for SEO or initial page rendering (ssr).
- Compression: Supports gzip and deflate compression for optimizing data transfer.
- Customizable Response Headers: Allows customization of HTTP response headers, including CORS headers.
- Timeout Handling: Automatically handles request timeouts to prevent hanging connections.
- Debugging: Provides optional debug logs to track server activity and HTML content.
HTTP Server
Serve single page application on the HTTP server.
import { HTTPServer } from '@mikosoft/spa-server';
const httpOpts = {
staticDir: 'dist',
indexFile: 'index.html',
urlRewrite: {}, // map URLs to directory: {url1: dir1, url2:dir2} NOTICE:The url i.e. the object key can contain regex chars like ^ $ * ...
port: process.env.PORT || 3000,
timeout: 5 * 60 * 1000, // if 0 never timeout
acceptEncoding: 'gzip', // gzip, deflate or ''
headers: {
// CORS Response Headers
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Authorization',
'Access-Control-Allow-Methods': 'OPTIONS, GET',
'Access-Control-Max-Age': '3600'
},
ssr: 'all', // none, all, botsonly
ssrConsole: false, // frontend JS logs in the backend
ssrModifier: (document) => { // modify document on the server side
document.title = 'Modified title';
const script = document.createElement('script');
script.textContent = "console.log('Dynamic script executed!')";
document.body.appendChild(script);
},
debug: false,
debugHTML: true
};
const httpServer = new HTTPServer(httpOpts);
httpServer.start();
OPTIONS (httpOpts)
- staticDir:string, - directory with static, frontend files (path relative to process.cwd())
- indexFile:string, - root HTML file (in the staticDir)
- urlRewrite:object, - map URLs to directory: {url1: dir1, url2:dir2} NOTICE:The url i.e. the object key can contain regex chars like ^ $ * ...
- port:number - HTTP Server port number
- timeout:number - ms to wait for response. If 0 then the HTTP connection will never close. Default is 5 minute
- acceptEncoding:string - gzip or deflate
- headers:object - custom server response headers
- ssr:'all'|'botsonly'|'none' - server side rendering
- ssrConsole:boolean - show frontend JS logs on the backend terminal
- ssrModifier:null|Function - modify document on the server side
- debug:boolean - print debug messages
- debugHTML:boolean - debug initial and postrender HTML
HTTP Server methods
- start() - start the HTTP server
- stop() - stop the HTTP server
- restart() - restart the HTTP server
Events
listening: Event emitted when the HTTP server starts listening for connections. close: Event emitted when the HTTP server closes. error: Event emitted when an error occurs in the HTTP server.
Contributing
Feel free to contribute by submitting issues and pull requests on GitHub.
Support
Contact on www.mikosoft.info
Licence
Copyright (C) 2023-present MikoSoft licensed under MIT .