Skip to content

Instantly share code, notes, and snippets.

@JozefFlakus
Created May 12, 2019 15:45
Show Gist options
  • Save JozefFlakus/bd4fca8373d853c1779f38a4a5dd030e to your computer and use it in GitHub Desktop.
Save JozefFlakus/bd4fca8373d853c1779f38a4a5dd030e to your computer and use it in GitHub Desktop.
Marble.js - one file example
import { createServer, combineRoutes, httpListener, r } from '@marblejs/core';
import { logger$ } from '@marblejs/middleware-logger';
import { bodyParser$ } from '@marblejs/middleware-body';
import { map, mapTo } from 'rxjs/operators';
// USERS API definition
const getUserList$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
mapTo({
body: [
{ id: '1', name: 'test_1' },
{ id: '2', name: 'test_2' },
],
})
)),
);
const getUser$ = r.pipe(
r.matchPath('/:id'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
map(req => ({
body: { id: req.params.id, name: 'test_1' },
}))
)),
);
const users$ = combineRoutes('/users', [
getUser$,
getUserList$,
]);
// ROOT API definition
const root$ = r.pipe(
r.matchPath('/'),
r.matchType('GET'),
r.useEffect(req$ => req$.pipe(
mapTo({ body: `API version: v1` }),
)),
);
const notFound$ = r.pipe(
r.matchPath('*'),
r.matchType('*'),
r.useEffect(notFoundEffect$),
);
const api$ = combineRoutes('/api/v1', [
root$,
users$,
notFound$
]);
// SERVER definition
const middlewares = [
logger$(),
bodyParser$(),
];
const effects = [
api$,
];
const server = createServer({
port: 1337,
hostname: '127.0.0.1',
httpListener: httpListener({ middlewares, effects }),
});
server.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment