@minieggs40/http-create-server
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

@minieggs40/http-create-server

A simple, functional wrapper over NodeJS builtin, http.createServer.

Why

I've made this 20 times at least for putting together quick hacky projects. Might as well reduce my time remaking this by publishing to NPM.

Install

$ npm i -S @minieggs40/http-create-server

Usage

import { createServer } from "@minieggs40/http-create-server";

const routes = [
  {
    test: /test/,
    method: "POST",
    handle: (req, res) => {
      res.json({ msg: `Post body received: ${JSON.stringify(req.body)}` });
    }
  }
];

const port = process.env.PORT || 8080;

const defaultHandle = (req, res) => {
  res.end("Error.");
};

const onListen = () => {
  console.log(`Server has been started on port ${port}.`);
};

const start = createServer({ routes, port, defaultHandle, onListen });
start();

Or even more simply.

import { createServer } from "@minieggs40/http-create-server";

const routes = [
  {
    test: /test/,
    method: "POST",
    handle: (req, res) => {
      res.json({ msg: `Post body received: ${JSON.stringify(req.body)}` });
    }
  }
];

const start = createServer({ routes });
start();

Package Sidebar

Install

npm i @minieggs40/http-create-server

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

8.55 kB

Total Files

5

Last publish

Collaborators

  • minieggs40