Skip to content

Instantly share code, notes, and snippets.

server.post("/register", (request, response) => {
const userName = request.body.userName;
// we can use request data to trigger positive
// or negative response, whatever we need
if (userName === "invalid") {
response.send(400, {
message: "Invalid user name."
});
} else {
const faker = require("faker");
server.get("/users/:id/articles", (request, response) => {
response.send(200, getArticlesForUser());
});
// you can create articles on the fly
// or prepare few articles upfront
// and just select them now
function getArticlesForUser() {
server.get("/users/:id/articles", (request, response) => {
const delay = faker.random.number({min: 400, max: 1000});
// delay response with fake articles
setTimeout(() => {
response.send(200, getArticlesForUser());
}, delay);
});
# fetch articles for user with ID "myUserId"
curl --verbose -X GET "http://localhost:4000/users/myUserId/articles"
# response:
# [{"title":"qui vero quo enim error","content":"...","createdAt":"2018-04-26T05:50:26.130Z"}, ...]
# try to register user with valid user name
curl --verbose -X POST http://localhost:4000/register -H 'content-type: application/json' -d '{"userName":"valid"}'
# response:
# ...
# < HTTP/1.1 200 OK
interface User {
fullName(): string;
adult(): boolean;
}
class Citizen implements User {
constructor(private firstName: string, private lastName: string, private age: number) {
}
fullName() {
@kazagkazag
kazagkazag / machine.js
Last active January 26, 2021 19:59
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions