Quick setup
The page describes a quick and basic usage of @marblejs/http module.
The very basic configuration consists of two steps: HTTP listener definition and HTTP server configuration.
Since version 4.0 all HTTP-related API's are moved and organized in a dedicated @marblejs/http package.
httpListener is the basic starting point of every Marble application. It includes definitions of all global middlewares and API effects.
http.listener.ts
1
import { httpListener } from '@marblejs/http';
2
import { logger$ } from '@marblejs/middleware-logger';
3
import { bodyParser$ } from '@marblejs/middleware-body';
4
import { api$ } from './api.effects';
5
6
const middlewares = [
7
logger$(),
8
bodyParser$(),
9
// middleware3$
10
// middleware4$
11
// ...
12
];
13
14
const effects = [
15
api$,
16
// endpoint2$
17
// endpoint3$
18
// ...
19
];
20
21
export const listener = httpListener({
22
middlewares,
23
effects,
24
});
Copied!
And here is our simple "hello world" endpoint.
api.effects.ts
1
import { r } from '@marblejs/http';
2
import { mapTo } from 'rxjs/operators';
3
4
export const api$ = r.pipe(
5
r.matchPath('/'),
6
r.matchType('GET'),
7
r.useEffect(req$ => req$.pipe(
8
mapTo({ body: 'Hello, world!' }),
9
)));
Copied!
To create Marble app instance, we can use createServer, which is a wrapper around Node.js server creator with much more possibilities and goods inside. When created, it won't automatically start listening to given port and hostname until you call its awaited instance.
index.ts
1
import { createServer } from '@marblejs/http';
2
import { IO } from 'fp-ts/lib/IO';
3
import { listener } from './http.listener';
4
5
const server = createServer({
6
port: 1337,
7
hostname: '127.0.0.1',
8
listener,
9
});
10
11
const main: IO<void> = async () =>
12
await (await server)();
13
14
main();
Copied!
To see Marble.js in action you can visit example usage of available module for a complete Marble.js app example.
We'll use TypeScript in the documentation but you can always write Marble apps in standard JavaScript (and any other language that transpiles to JavaScript).
To test run your server you can install typescript compiler and ts-node:
1
$ yarn add typescript ts-node
Copied!
Add the following script to your package.json file:
1
"scripts": {
2
"start": "ts-node index.ts"
3
}
Copied!
Now go ahead, create index.ts, http.listener.ts, api.effects.ts modules in your project and run your server:
1
$ yarn start
Copied!
Finally test your "functional" server by visiting http://localhost:1337
For more API specific details about server bootstrapping, visit createServer API reference
In the next HTTP chapter you will learn how to create basic Marble.js endpoints using Effects, how to build and compose middlewares and how to build a basic REST API routing.
Last modified 2mo ago
Copy link