1 | # Gaia Mock Server
|
2 |
|
3 | A [node.js][nodejs] mock server for the [Gaia][gaia] REST service.
|
4 |
|
5 | This mockserver can act as a simple replacement for the [Gaia][gaia] REST
|
6 | service in tests and as a standalone server. This mock server serve static
|
7 | files stored in the projects `data` directory.
|
8 |
|
9 |
|
10 |
|
11 | ## Run as a standalone server
|
12 |
|
13 | Check out this repo, in the repo run the following:
|
14 |
|
15 | ```bash
|
16 | $ npm install
|
17 | $ npm start
|
18 | ```
|
19 |
|
20 | The mock server will then be running on: http://localhost:9050/.
|
21 |
|
22 |
|
23 |
|
24 | ## Use as a library
|
25 |
|
26 | This mock server can be included in your own code and started by your own code.
|
27 | You can then controll and listen on what this application does within your own
|
28 | program.
|
29 |
|
30 |
|
31 | ### Installation
|
32 |
|
33 | ```bash
|
34 | $ npm install gaia-mock
|
35 | ```
|
36 |
|
37 | ### Constructor
|
38 |
|
39 | This constructs a new mock server:
|
40 |
|
41 | ```js
|
42 | var server = new GaiaMockServer();
|
43 | ```
|
44 |
|
45 |
|
46 | ### Events
|
47 |
|
48 | The server emit the following events:
|
49 |
|
50 | * `listen` - When the server is up running and listening for upcomming requests.
|
51 | The first parameter to the `callback` is the port number its listening on.
|
52 | * `close` - When the server has closed down.
|
53 | * `request` - When the server receives a request. The first parameter to the
|
54 | `callback` is the URL which was requested.
|
55 |
|
56 |
|
57 | ### API
|
58 |
|
59 | The server has the following methods:
|
60 |
|
61 |
|
62 | #### .listen(port)
|
63 |
|
64 | Starts the server. `port` is the port number the server should listen on. If no
|
65 | value is given for `port` the server will attach itself to a available port.
|
66 |
|
67 |
|
68 | #### .close()
|
69 |
|
70 | Closes down the server.
|
71 |
|
72 |
|
73 | ### Example
|
74 |
|
75 | ```js
|
76 | var MockServer = require('gaia-mock');
|
77 |
|
78 | var gaia = new MockServer();
|
79 |
|
80 | gaia.on('listen', function (port) {
|
81 | console.log('server is running on port: ' + port);
|
82 | });
|
83 |
|
84 | gaia.on('close', function (port) {
|
85 | console.log('server stopped');
|
86 | });
|
87 |
|
88 | gaia.on('request', function (url) {
|
89 | console.log('server got a request on: ' + url);
|
90 | });
|
91 |
|
92 | gaia.listen(8080);
|
93 |
|
94 | // gaia.close();
|
95 | ```
|
96 |
|
97 |
|
98 |
|
99 | [nodejs]: http://http://nodejs.org/ "node.js"
|
100 | [gaia]: https://github.com/amedia/gaia "Gaia"
|