This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface User { | |
| fullName(): string; | |
| adult(): boolean; | |
| } | |
| class Citizen implements User { | |
| constructor(private firstName: string, private lastName: string, private age: number) { | |
| } | |
| fullName() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Available variables: | |
| // - Machine | |
| // - interpret | |
| // - assign | |
| // - send | |
| // - sendParent | |
| // - spawn | |
| // - raise | |
| // - actions |
OlderNewer