Giter Site home page Giter Site logo

internetarchive / file_server_plus Goto Github PK

View Code? Open in Web Editor NEW
2.0 14.0 0.0 37 KB

`deno` static file webserver, clone of `file_server.ts`, PLUS an additional final "404 handler" to run arbitrary JS/TS

License: GNU General Public License v3.0

TypeScript 100.00%
deno fileserver httpd static-files webserver

file_server_plus's Introduction

file_server_plus

This is a deno static file webserver, copied from: file_server.ts, with an additional final "404 handler" ability to run arbitrary JS/TS, path match routes for dynamically running JS/TS, etc.

This can be useful for Single Page Application type setups (and more) for when you want an URL path to run certain code.

The script should be relative from the static files directory.

For example, if you have a public/ subdir in a repository of files that you'd like to serve static files from, and a JS file for additional "routes" in the parent dir called routes.js, you'd run this server like:

cd public/
../routes.js

Full Example routes.js

#!/usr/bin/env -S deno run --allow-net --allow-read

import main from 'https://deno.land/x/file_server_plus/mod.ts'

// Any request about to 404 arrives here and you get final ability to handle/run code or 404
globalThis.finalHandler = (req) => {
  const headers = new Headers()
  try {
    const parsed = new URL(req.url)
    if (parsed.pathname.startsWith('/details/')) {
      // main website /details/[SOMETHING] logical rewrite back to top page
      headers.append('content-type', 'text/html')
      return Promise.resolve(new Response(
        Deno.readTextFileSync('./index.html'),
        { status: 200, headers },
      ))
    }
  } catch (error) {
    // some kind of error -- issue 500
    console.warn(error)
    return Promise.resolve(new Response(`Server Error: ${error.message}`, { status: 500, headers }))
  }

  // not a route we want to handle -- issue 404
  headers.append('content-type', 'text/html')
  return Promise.resolve(new Response('<center><br><br><br><br>Not Found</center>', { status: 404, headers }))
}

main()

Compare minimal changes with stock file_server.ts

colordiff  *.ts

Author's Note: upgrade this package to latest version of file_server.ts

# get latest version from mothership
wget -qO  file_server.ts \
  https://raw.githubusercontent.com/denoland/deno_std/main/http/file_server.ts

cp  file_server.ts  mod.ts

# Re-add our small modification patch into the latest stock file_server.ts.
# ( NOTE: previously did, after intitially made manual modifications: )
#     diff -u file_server.ts mod.ts | tee plus.patch
patch  mod.ts  plus.patch

# update these both as appropriate:
VERSION_STD=0.155.0
VERSION=0.2.3

# change `from "./`  strings to `from "https://deno.land/std@$VERSION/http/`
# change `from "../` strings to `from "https://deno.land/std@$VERSION/`
perl -i -pe "s=from \x22./=from \x22https://deno.land/std\@$VERSION_STD/http/=" mod.ts
perl -i -pe "s=from \x22../=from \x22https://deno.land/std\@$VERSION_STD/="     mod.ts

# review our changes
colordiff -U5 *.ts

# Update a new version to https://deno.land/x
git commit -m 'updated to latest file_server.ts' .
git push
git tag         $VERSION
git push origin $VERSION

file_server_plus's People

Contributors

traceypooh avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.