npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fun-http

v0.2.1

Published

Functional HTTP server for FUN!

Downloads

8

Readme

:monkey_face: Fun(ctional) Http

npm version CircleCI

Possibly the easiest way to start a HTTP server in Node.js.

HTTP server should be as stateless as possible like functions. We should write request handlers in pure funtion. Let's treat return value as responses and use promises to wrap async operations.

Usage

fun-http supports Node >= 4.

As a CLI

  1. Install as global package

    $ npm install -g fun-http
  2. Define the server, only need to export a function.

    // server.js
    export default async function (req) {
      return 'Hello, World!';
    }
  3. Fire the server, and tada~ :tada:

    $ fun-http server.js
    $ curl -i localhost:3000
    
    HTTP/1.1 200 OK
    Date: Sun, 12 Jun 2016 22:12:41 GMT
    Connection: keep-alive
    Content-Length: 13
    
    Hello, World!

As a module

npm install --save fun-http
const FunHttp = require('fun-http').FunHttp;
const app = new FunHttp();

app.use(function (req) {
  return 'Hello, World!';
});

app.listen(3000, () => {
  console.log('server started');
});

Visit localhost:3000 will display "Hello, World!".

You can also return promise in request handlers, the resolved value will become the body of response.

app.use(function (req) {
  return Promise.resolve('Hello, World!');
});

If returned value (or promise resolved value) is not a number or string, it will be parsed into JSON string using JSON.stringify.

Customize Response

fun-http will look for special structure on values returned from request handler to customize response's status and headers.

app.use(function () {
  return {
    status: 404,
    json: {
      reason: 'not found'
    }
  };
});
$ curl -i localhost:3000

HTTP/1.1 404 Not Found
Date: Fri, 10 Jun 2016 00:14:34 GMT
Connection: keep-alive
Content-Length: 22

{"reason":"not found"}

For custom headers:

app.use(function (req) {
  return {
    headers: {
      'x-really-awesome': 'yes!'
    }
  };
});
$ curl -i localhost:3000

HTTP/1.1 200 OK
x-really-awesome: yes!
Date: Fri, 10 Jun 2016 00:14:02 GMT
Connection: keep-alive
Content-Length: 0

You can also force fun-http to return text:

app.use(function (req) {
  return {
    text: 'I just want to tell you...'
  };
});

Middleware

fun-http supports middleware similar to Koa. Calling next will invoke next middleware and return a promise wrapping the return value of that middleware.

app.use(function (req, next) {
  return next()
    .then(name => {
      return {hello: name}; // name === 'World'
    });
});

app.use(function () {
  return 'World';
});

Paire with co, you can make everything cleaner.

const co = require('co');

app.use(co.wrap(function *(req, next) {
  const name = yield next();
  return {hello: name};
}));

app.use(function () {
  return 'World';
});

Or you can even use it with async/await functions:

app.use(async function (req, next) {
  const name = await next();
  return {hello: name};
});

app.use(function () {
  return 'World';
});

Of cause, Node.js doesn't currently support async/await functions, you will need to use transpiler like Babel to transpile the source.

Examples

Check out examples.

TypeScript

Did I tell you fun-http is written using TypeScript?