• Stars
    star
    1,026
  • Rank 44,566 (Top 0.9 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Dependency free file server for single page app development

Servør

A dependency free dev server for modern web application development

A very compact but capable static file server with https, live reloading, gzip and other useful features to support modern web app development on localhost and over a local network. The motivation here was to write a package from the ground up with no dependencies; using only native, node and browser APIs to do some specific tasks with minimal code.

Servør can be invoked via the command line or programmatically using the node API.

Quickstart Example

The following command instructs servør to; clone perflink, start a server at the project root, open the url in a browser, open the source code in an editor and reload the browser when files change.

npx servor gh:lukejacksonn/perflink --browse --editor --reload

Most features are disabled by default but you can customize behaviour by passing positional arguments and flags to enable features.


servor

Features

  • 🗂 Serves static content like scripts, styles, images from a given directory
  • ♻️ Reloads the browser when project files get added, removed or modified
  • 🗜 Uses gzip on common filetypes like html, css, js and json
  • 🔐 Supports https and http2 with trusted self signed certificates
  • 🖥 Redirects all path requests to a single file for frontend routing
  • 📦 Accepts both HTML and JavaScript files as the root file for a directory
  • 🔎 Discovers freely available ports to start if the default is in use
  • 📄 Renders directory listing for urls ending with a trailing slash
  • 🗃 Opens browser tab and code editor to streamline quick start

CLI Usage

Run as a terminal command without adding it as a dependency using npx:

npx servor <root> <fallback> <port>

You can pass a GitHub repo as <root> using the syntax gh:<user>/<repository>

  • <root> path to serve static files from (defaults to current directory .)
  • <fallback> the file served for all non-file requests (defaults to index.html)
  • <port> what port you want to serve the files from (defaults to 8080)

Optional flags passed as non-positional arguments:

  • --browse causes the browser to open when the server starts
  • --reload causes the browser to reload when files change
  • --secure starts the server with https using generated credentials
  • --silent prevents the server node process from logging to stdout
  • --module causes the server to wrap the root in script type module tags
  • --static causes the server to route nested index files if they exist
  • --editor opens a code editor (currently only vscode) at the project root

Example usage with npm scripts in a package.json file after running npm i servor -D:

{
  "devDependencies": {
    "servor": "4.0.0"
  },
  "scripts": {
    "start": "servor www index.html 8080 --reload --browse"
  }
}

Generating Credentials

NOTE: This process depends on the openssl command existing (tested on macOS and linux only)

The files servor.crt and servor.key need to exist for the server to start using https. If the files do not exist when the --secure flag is passed, then certify.sh is invoked which:

  • Creates a local certificate authority used to generate self signed SSL certificates
  • Runs the appropriate openssl commands to produce:
    • a root certificate (pem) so the system will trust the self signed certificate
    • a public certificate (crt) that the server sends to clients
    • a private key for the certificate (key) to encrypt and decrypt traffic

Adding credentials to the trusted store

NOTE: This process depends on the sudo and security commands existing (tested on macOS only)

For the browser to trust self signed certificates the root certificate must be added to the system trusted store. This can be done automatically by running sudo servor --secure which:

  • Adds the root certificate to the system Keychain Access
  • Prevents the "⚠️ Your connection is not private" screen
  • Makes the 🔒 icon appear in the browsers address bar

The approach was adopted from @kingkool68/generate-ssl-certs-for-local-development

API Usage

Use servor programmatically with node by requiring it as a module in your script:

const servor = require('servor');
const instance = await servor({
  root: '.',
  fallback: 'index.html',
  module: false,
  static: false,
  reload: false,
  inject: ''
  credentials: null,
  port: 8080,
});

The servor function accepts a config object with optional props assigned the above default values if none are provided. Calling the servor function starts up a new server and returns an object describing its configuration.

const { url, root, protocol, port, ips } = await servor(config);

Inject

The inject property accepts a string that gets appended to the servers root document (which is index.html by default). This could be used to inject config or extend the development servers behavior and capabilities to suit specific environments.

const config = require('package.json');
servor({ inject: `<script>window.pkg=${config}</script>` });

Credentials

The credentials property accepts an object containing the entries cert and key which must both be valid for the server to start successfully. If valid credentials are provided then the server will start serving over https.

It is possible to generate the appropriate credentials using the --secure CLI flag.

Notes

Thanks to all the contributors to this projects so far. If you find a bug please create an issue or if you have an idea for a new feature then fork the project and create a pull request. Let me know how you are using servør on twitter.

More Repositories

1

react-slack-clone

Complete chat application, built with Chatkit | by @lukejacksonn
JavaScript
1,349
star
2

perflink

Low friction JavaScript benchmarks that you can share via URL
JavaScript
1,206
star
3

ijk

Transforms arrays into virtual dom trees; a terse alternative to JSX and h
JavaScript
467
star
4

oceanwind

Compiles tailwind shorthand into css at runtime. Succeeded by Twind.
JavaScript
264
star
5

GreedyNav

A responsive navigation menu that stacks items into a dropdown menu when they overflow
JavaScript
232
star
6

es-react

ES6 module exposing the latest version of react and react-dom
JavaScript
231
star
7

csz

Runtime CSS modules with SASS like preprocessing
JavaScript
218
star
8

monacode

An es-module wrapper around the monaco editor and prettier
JavaScript
165
star
9

create-es-react-app

A create-react-app like template using only es-modules (no build step).
JavaScript
103
star
10

hyperapp-pwa

A minimalist, progressive web app compliant template for hyperapp projects
JavaScript
72
star
11

domz

A proxy function to help build virtual DOM trees from functions
JavaScript
62
star
12

hyperapp-electron

A minimal electron app starter with hyperapp, livereload and redux devtools
JavaScript
51
star
13

Actuate

One line easy actuation of CSS animation sequences
JavaScript
41
star
14

chattery

A GitHub action that creates chatrooms for pull requests
Ruby
29
star
15

esm

A single file es module prototyping tool
JavaScript
29
star
16

blub

[POC] resolving es module dependency graphs on the server
JavaScript
27
star
17

deployable

A ready-to-deploy static web app template and tutorial
HTML
25
star
18

hyperapp-firebase-auth

🔥 Drop in authentication for hyperapps using firebase
JavaScript
19
star
19

Lorem

Generates random lorem ipsum snippets
JavaScript
19
star
20

Malette

A color picker tool for the google material design palette
JavaScript
18
star
21

cube

Rubik's Cube simulator with F2L/OLL/PLL algorithms
JavaScript
16
star
22

hyperapp-wiki

Example of how hyperapp could be used to serve its on wiki with gh-pages
HTML
13
star
23

blog

A forkable personal blog that runs on GitHub pages. Demo 👉
JavaScript
12
star
24

hyperapp-hn

First attempt at a community hacker news PWA built with HyperApp
JavaScript
10
star
25

vuid

❄️ Very unique identifiers in the browser using web cryptography (144b)
JavaScript
10
star
26

hyperapp-fetch

A component to fetch and cache data before rendering a view
JavaScript
10
star
27

tetris

The classic NES Tetris built with hyperapp
JavaScript
9
star
28

ipsums

Generate random amounts of lorem ipsum text
JavaScript
8
star
29

framer

Easily apply a frame overlay to an avatar photo
JavaScript
8
star
30

hyperapp-starter

A minimalist starter for hyperapp projects
JavaScript
6
star
31

Protohype

A set of aliases for commonly used CSS properties and values
CSS
5
star
32

hyperapp-router

A frontend router for hyperapp view switching
JavaScript
4
star
33

preroll

An opinionated web project builder with dev server
JavaScript
3
star
34

streamer

🚰 Streams of reasonably interesting data published reasonably stochastically
JavaScript
2
star
35

fb-sdk

Load and queue calls to the facebook sdk
JavaScript
2
star
36

dankon

🗺 Learn to express your gratitude in 140+ different languages
JavaScript
2
star
37

huy

useful h function ui elements
JavaScript
2
star
38

plural

picture-in-picture interface for youtube videos
JavaScript
2
star
39

hyperapp-two

A multi-page and multi-component boilerplate hyperapp project
JavaScript
2
star
40

xs

An extra small, stand alone web application template
HTML
1
star
41

youtube-eco

A chrome extension that optimizes youtube streaming quality
JavaScript
1
star
42

hyperapp-unite

A utility function for merging reducers, effects and routes with safe namespacing
JavaScript
1
star
43

Stash

A jQuery plugin that can instigate application/octet-stream downloads of local content
JavaScript
1
star
44

Browver

A JavaScript plugin that detects if a browser is too and points to the latest browser downloads..
JavaScript
1
star
45

lukejacksonn.github.io

My personal homepage to be hosted at http://lukejacksonn.com
HTML
1
star
46

gitinit

Create a new git repo based on an already existing repository
JavaScript
1
star
47

feeder

My awesome pusher feeds app (test)
JavaScript
1
star
48

loginn

A set of lambda functions and frontend components to aid authentication
JavaScript
1
star
49

create-pusher-app

An experiment with yeoman, hyperapp and the Pusher Feeds API
JavaScript
1
star
50

lukejacksonn

1
star