xserver

A simple http server.

Usage no npm install needed!

<script type="module">
  import xserver from 'https://cdn.skypack.dev/xserver';
</script>

README

XServer

A very simple HTTP server for Node.js. The point is to deliever files and respond to regular expression routing.

Basics of the Server:

  1. When a request is sent to the server it matches the URI pattern to any routes you have set up to run. It is up to the routed method to return a response.
  2. If no routes are found it will try to find a file and return it.
  3. If no files are found it will return a 404 status response code.

Installation

npm install xserver

Example

var XServer = require("./xserver.js");

var xs = new XServer({
  directories: ["./public"],
  temporaryDirectory: "./tmp",
  port: 3003
});

xs.get(/^$/, function ( directive ) {
    directive.sendText("XServer");
});

xs.get(/^coffee$/, function ( directive ) {
  directive.setData("Hello World!");
  directive.next();
});

xs.get(/^chain$/, 
function ( directive ) {
    directive.setData("Chain 1");
    directive.next();
},
function ( directive ) {
    directive.setData(directive.data + "\nChain 2");
    directive.setContentType("txt");
    directive.next();
});

xs.get(/^download-file$/, function ( directive ) {
  directive.perpareFile("./package.json", function ( err ) {
    if ( err ) return directive.sendStatus(500);
    directive.send();
  });
});

xs.get(/^say\/(.*)$/, function ( directive, segment ) {
  directive.sendText("Server says: " + segment);
});

xs.start();