---
id: getting-started
title: Getting Started
sidebar_label: Getting Started
---

_Http4ts_ is a minimal HTTP library for JavaScript environments ([Node.js](https://nodejs.org), [Deno](https://Deno.land/) etc.) implementing the pattern of [server as a function](https://monkey.org/~marius/funsrv.pdf). In _http4ts_, a server is just a function with the following signature:

```ts
type HttpHandler = (req: HttpRequest) => HttpResponse | Promise<HttpResponse>;
```

See the simple server application examples, one for [deno](https://github.com/http4ts/http4ts/tree/master/src/deno/examples) and another for [Node.js](https://github.com/http4ts/http4ts/tree/master/src/node/examples).

# Installation

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

<Tabs
  defaultValue="node"
  values={[
    { label: 'Node.js', value: 'node', },
    { label: 'Deno', value: 'deno', }
  ]
}>
<TabItem value="node">

*Http4ts* is available via npm. You can install it using the following command:

```
npm install http4ts
```

### Binding to Node.js

In order to use this library in Node.js, you have to bind the `HttpHandler` to the Node.js HTTP server. _Http4ts_ supplies a function called [`toNodeRequestListener`](https://github.com/http4ts/http4ts/blob/master/src/node/server.ts) to bind an `HttpHandler` to the Node.js server.

```ts
import * as http from "http";

import {
  HttpRequest,
  HttpStatus,
  toNodeRequestListener,
  stringBody,
  res
} from "http4ts";

async function handler(req: HttpRequest) {
  // 1. Write the handler as a function that returns response
  return res({
    body: stringBody("Hello world!"),
    status: HttpStatus.OK
  });
}

const server = http.createServer(
  toNodeRequestListener(handler) // 2. Connect the handler to the node.js server
);

const hostname = "127.0.0.1";
const port = 3000;

server.listen(port, hostname, () => {
  // 3. Start your node server as you were before
  console.log(`Server running at http://${hostname}:${port}/`);
});
```

</TabItem>
<TabItem value="deno">

In Deno, it is possible to import the library via its url. You can use *Http4ts* by importing the following url:
```
https://deno.land/x/http4ts@v0.1.2/mod.ts
```

### Binding to Deno

In order to use this library in Deno, you have to bind the `HttpHandler` to the Deno HTTP server. *Http4ts* supplies a function called [`toDenoRequestListener`](https://github.com/http4ts/http4ts/blob/master/src/deno/server.ts) to bind an `HttpHandler` to the server.

```ts
import { listenAndServe } from "https://deno.land/std/http/server.ts";

import {
  HttpRequest,
  HttpStatus,
  toDenoRequestListener,
  stringBody,
  res
} from "https://deno.land/x/http4ts@v0.1.2/mod.ts";

async function handler(req: HttpRequest) {  // 1. Write the handler as a function that returns response
  return res({
    body: stringBody("Hello world!"),
    status: HttpStatus.OK
  });
}

console.log("Listening on http://localhost:8000");
await listenAndServe({ port: 8000 }, toDenoRequestListener(handler));
```

You can also run this example by executing the following command in your shell environment:

```
deno run --allow-net https://deno.land/x/http4ts@v0.1.2/examples/readme-example.ts
```

</TabItem>
</Tabs>

## Example Project

We have implemented the famous [realworld backend](https://github.com/gothinkster/realworld) for you to compare the code with other http libraries in node.js. You can find this example [here](https://github.com/http4ts/http4ts-realworld-example-app).
