1 | // API Specification
|
2 | // This document is to define server's stable API
|
3 | // - Main function
|
4 | // - options
|
5 | // - middleware
|
6 | // - context
|
7 | // - router
|
8 | // - reply
|
9 | // - utils
|
10 |
|
11 |
|
12 |
|
13 | // Main function
|
14 | const server = require('server');
|
15 |
|
16 | // Definition: an ASYNC function that accepts options and/or middleware
|
17 | server(opts, mid1, mid2, ...).then(...);
|
18 |
|
19 | // Properties (defined below)
|
20 | server.router;
|
21 | server.reply;
|
22 | server.utils;
|
23 |
|
24 |
|
25 |
|
26 | // Options
|
27 | const ops = {
|
28 |
|
29 | // Simple options
|
30 | port,
|
31 | engine,
|
32 | public,
|
33 | secret,
|
34 | log,
|
35 |
|
36 | // TODO: MISSING MANY HERE; THIS PART IS NOT YET STABLE
|
37 |
|
38 | // Plugins options
|
39 | core,
|
40 | parser,
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 | // Middleware
|
46 | // Definition: (a)sync function, accepts the Context and returns a reply
|
47 | const mid1 = ctx => { ... };
|
48 | const mid2 = async ctx => { ... };
|
49 |
|
50 | // Return types
|
51 | // String => HTML or PLAIN response
|
52 | const mid = ctx => 'Hello world';
|
53 | // Array/Object => JSON response
|
54 | const mid = ctx => ['I', 'am', 'json'];
|
55 | const mid = ctx => ({ hello: 'world' }); // To return an object the extra () is needed
|
56 | // Reply instance
|
57 | const mid = ctx => server.reply.send('hello world');
|
58 |
|
59 |
|
60 |
|
61 | // Context
|
62 | // Definition: all of the currently known data. Varies depending on location
|
63 | // NOTE: there are more properties, but they are not considered stable
|
64 | ctx.options, // the specified or inherited options
|
65 | ctx.log, // a method to log things in several levels
|
66 | ctx.reply, // same as above
|
67 | ctx.utils, // utilities
|
68 | ctx.server, // the currently running server instance
|
69 |
|
70 | // For middleware/routers
|
71 | ctx.data, // the parsed body if it's a POST request
|
72 | ctx.params, // NOTE: NOT YET, rely on ctx.req.params so far
|
73 | ctx.query, // NOTE: NOT YET, rely on ctx.req.query so far
|
74 | // ...
|
75 |
|
76 | // Non-stable (will change at some point in the future)
|
77 | ctx.req, // express request; considering removing/changing it in 1.1
|
78 | ctx.res, // express response; not useful anymore, use server.reply instead
|
79 |
|
80 |
|
81 |
|
82 | // Router
|
83 | const router = server.router;
|
84 | const router = require('server/router');
|
85 |
|
86 | // Definition: handle the different methods and paths requested
|
87 | router.get('/', mid1);
|
88 | router.post('/users', mid2);
|
89 | router.put('/users/:id', mid3);
|
90 |
|
91 | // Methods (REST methods not explained):
|
92 | router.get;
|
93 | router.post;
|
94 | router.put;
|
95 | router.del;
|
96 | router.socket; // Handle websocket calls
|
97 | router.error; // Handle errors further up in the chain
|
98 |
|
99 |
|
100 |
|
101 | // Reply
|
102 | const reply = server.reply;
|
103 | const reply = require('server/reply');
|
104 |
|
105 | // Definition: handle the response from your code
|
106 | // Note: it MUST be returned from the middleware or it won't be executed
|
107 | reply.cookie;
|
108 | reply.download;
|
109 | reply.end;
|
110 | reply.file;
|
111 | reply.header;
|
112 | reply.json;
|
113 | reply.jsonp;
|
114 | reply.redirect;
|
115 | reply.render;
|
116 | reply.send;
|
117 | reply.status;
|
118 | reply.type;
|
119 |
|
120 |
|
121 |
|
122 | // Utils
|
123 | const utils = server.utils;
|
124 | const utils = require('server/utils'); // NOT YET AVAILABLE
|
125 |
|
126 | // Definition: some extra utilities to make development easier
|
127 | utils.modern; // Make express middleware work with server.js
|