LogoLogo
ChangelogGitHubTwitterGitter
v4.x
v4.x
  • Marble.js
  • Getting started
    • Installation
    • Quick setup
  • HTTP
    • Effects
    • Middlewares
    • Routing
    • Errors
    • Output
    • Context
    • Advanced
      • Logging
      • Validation
      • Server Events
      • Streaming
      • Continuous mode
  • Messaging
    • Core concepts
      • Events
      • Effects
    • Microservices
      • AMQP (RabbitMQ)
      • Redis Pub/Sub
    • CQRS
    • WebSockets
  • Testing
    • HTTP routes testing
  • Other
    • How does it glue together?
    • Migration guides
      • Migration from version 3.x
      • Migration from version 2.x
      • Migration from version 1.x
    • API reference
      • @marblejs/core
        • bindTo
        • bindEagerlyTo
        • createEvent
        • createContextToken
        • operator: matchEvent
        • operator: use
        • operator: act
      • @marblejs/http
        • httpListener
        • r.pipe
        • combineRoutes
        • createServer
      • @marblejs/messaging
        • eventBus
        • messagingClient
        • createMicroservice
        • reply
      • @marblejs/websockets
        • webSocketListener
        • operator: broadcast
        • operator: mapToServer
      • @marblejs/middleware-multipart
      • @marblejs/middleware-cors
      • @marblejs/middleware-io
      • @marblejs/middleware-logger
      • @marblejs/middleware-body
      • @marblejs-contrib/middleware-jwt
        • Token signing
      • @marblejs-contrib/middleware-joi
    • Style Guide
    • FAQ
Powered by GitBook
On this page
  1. Getting started

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.

import { httpListener } from '@marblejs/http';
import { logger$ } from '@marblejs/middleware-logger';
import { bodyParser$ } from '@marblejs/middleware-body';
import { api$ } from './api.effects';

const middlewares = [
  logger$(),
  bodyParser$(),
  // middleware3$
  // middleware4$
  // ...
];

const effects = [
  api$,
  // endpoint2$
  // endpoint3$
  // ...
];

export const listener = httpListener({
  middlewares,
  effects,
});

And here is our simple "hello world" endpoint.

import { r } from '@marblejs/http';
import { mapTo } from 'rxjs/operators';

export const api$ = r.pipe(
  r.matchPath('/'),
  r.matchType('GET'),
  r.useEffect(req$ => req$.pipe(
     mapTo({ body: 'Hello, world!' }),
  )));
import { createServer } from '@marblejs/http';
import { IO } from 'fp-ts/lib/IO';
import { listener } from './http.listener';

const server = createServer({
  port: 1337,
  hostname: '127.0.0.1',
  listener,
});

const main: IO<void> = async () =>
  await (await server)();

main();

To test run your server you can install typescript compiler and ts-node:

$ yarn add typescript ts-node

Add the following script to your package.json file:

"scripts": {
  "start": "ts-node index.ts"
}

Now go ahead, create index.ts, http.listener.ts, api.effects.ts modules in your project and run your server:

$ yarn start
PreviousInstallationNextEffects

Last updated 3 years ago

To create Marble app instance, we can use , 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.

To see Marble.js in action you can visit usage of available module for a complete Marble.js app example.

We'll use in the documentation but you can always write Marble apps in standard JavaScript (and any other language that transpiles to JavaScript).

Finally test your "functional" server by visiting

For more API specific details about server bootstrapping, visit API reference

In the next HTTP chapter you will learn how to create basic Marble.js endpoints using , how to build and compose middlewares and how to build a basic REST API routing.

createServer
example
TypeScript
http://localhost:1337
createServer
Effects