/
typescript-server.ts
79 lines (70 loc) · 2.12 KB
/
typescript-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Most type annotations in this file are not strictly necessary but are
* included for this example.
*
* To run this example execute the following commands to install typescript,
* transpile the code, and start the server:
*
* npm i -g typescript
* tsc examples/typescript-server.ts --target es6 --module commonjs
* node examples/typescript-server.js
*/
import fastify, { FastifyInstance, RouteShorthandOptions } from '../fastify';
import { Server, IncomingMessage, ServerResponse } from 'http';
// Create an http server. We pass the relevant typings for our http version used.
// By passing types we get correctly typed access to the underlying http objects in routes.
// If using http2 we'd pass <http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>
const server: FastifyInstance<
Server,
IncomingMessage,
ServerResponse
> = fastify({ logger: true });
// Define interfaces for our request. We can create these automatically
// off our JSON Schema files (See TypeScript.md) but for the purpose of this
// example we manually define them.
interface PingQuerystring {
foo?: number;
}
interface PingParams {
bar?: string;
}
interface PingHeaders {
a?: string;
}
interface PingBody {
baz?: string;
}
// Define our route options with schema validation
const opts: RouteShorthandOptions = {
schema: {
body: {
type: 'object',
properties: {
pong: {
type: 'string'
}
}
}
}
};
// Add our route handler with correct types
server.post<{
Querystring: PingQuerystring;
Params: PingParams;
Headers: PingHeaders;
Body: PingBody;
}>('/ping/:bar', opts, (request, reply) => {
console.log(request.query); // this is of type `PingQuerystring`
console.log(request.params); // this is of type `PingParams`
console.log(request.headers); // this is of type `PingHeaders`
console.log(request.body); // this is of type `PingBody`
reply.code(200).send({ pong: 'it worked!' });
});
// Start your server
server.listen({ port: 8080 }, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`server listening on ${address}`)
});