Skip to content

sugojs/sugo-serve-static

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@sugo/serve-static

Handler for serving static files. Router agnostic. Recommended use with @sugo/router

serveStatic

Returns a middleware that reads and sends files.

Options

  • @param {\string} dir: The directory in which the files are stored.
  • @param {\string} baseUrl: The base url on the request to indicate that a static file is requested. If baseUrl is /base and the request url is /base/bar/foo.txt, then the file to be search will correspond to the path /bar/foo.txt.
  • @param {\string} notFoundRedirectPath: The filepath to be served if the requested file was not found. Can be used to render a 404 page. If not set, then it will throw an error when the file is not found. It this path is not found, it will also throw an error.

Requirements

node version >= 8.12.0

How to install

npm install --save @sugo/serve-static

Requesting a directory

If the user request a directory instead of a file, the middleware will search for a index.html in that directory.

Example - Server

import { createServer } from '@sugo/server';
import Router from '@sugo/router';
import { resolve } from 'path';
import serveStatic from '@sugo/serve-static';

const server = createServer((req: SuGoRequest, res: SuGoResponse) => router.handle(req, res)).useMiddleware(
  serveStatic({
    dir: resolve(__dirname, './public'),
  }),
);

Example - With routes

import { createServer } from '@sugo/server';
import Router from '@sugo/router';
import { resolve } from 'path';
import serveStatic from '@sugo/serve-static';

const router = new Router();
router.get(
  '/public/(.*)',
  serveStatic({
    baseUrl: '/public',
    dir: resolve(__dirname, './public'),
    notFoundRedirectPath: '404.html',
  }),
);
const server = createServer((req: SuGoRequest, res: SuGoResponse) => router.handle(req, res));

Example - NodeHttp

import * as http from 'http';
import serveStatic from '@sugo/serve-static';

const middleware = serveStatic({
  dir: resolve(__dirname, './public'),
});
const server = http.createServer((req, res) => middleware(req, res));

**Example - Hosting a Single Page Application (ReactJS, Angular, VueJS) **

import { createServer } from '@sugo/server';
import Router from '@sugo/router';
import { resolve } from 'path';
import serveStatic from '@sugo/serve-static';

const router = new Router();
router.get(
  '/webapp/(.*)',
  serveStatic({
    baseUrl: '/webapp',
    dir: resolve(__dirname, './webapp'),
    notFoundRedirectPath: 'index.html',
  }),
);
const server = createServer((req: SuGoRequest, res: SuGoResponse) => router.handle(req, res));

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors