You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple command-line JSON server. If you need to stub out an API locally, this is the tool you need. It's lightweight, works on Mac, Windows, and Linux, and requires no setup.
3
+
A simple command-line JSON server that also emulates a REST API. If you need to stub out an API locally, this is the tool you need. It's lightweight, works on Mac, Windows, and Linux, and requires no setup other than a JSON data file.
4
+
5
+
## Usage
4
6
5
7
Create a JSON file called `data.json`:
6
8
@@ -19,27 +21,115 @@ Create a JSON file called `data.json`:
19
21
}
20
22
```
21
23
22
-
Then run `demo-api`.
24
+
Then run `demo-api`. The server starts up.
23
25
24
-
Visit `http://localhost:8080` in the browser to see the file served as JSON.
26
+
Visit `http://localhost:8080` in the browser to serve the entire file as a JSON response.
25
27
26
-
In addition, since the JSON file is structured like a typical REST API data structure, the following will work:
28
+
In addition, since the JSON file is structured like a typical REST API data structure, the following endpoints will work:
27
29
28
30
*`GET http://localhost:8080/notes` - retrive all "notes"
29
31
*`GET http://localhost:8080/notes/1` - retrive the note with the `id` of `1`
30
-
*`POST http://localhost:8080/notes/` - Create a new note. This modifies the `data.json` file.
32
+
*`POST http://localhost:8080/notes/` with JSON payload - Create a new note. This modifies the `data.json` file.
33
+
*`PUT/PATCH http://localhost:8080/notes/1` with JSON payload - update the note with the `id` of `1`. This modifies the `data.json` file.
31
34
*`DELETE http://localhost:8080/notes/1` - Delete the note with the `id` of `1`. This modifies the `data.json` file.
32
35
36
+
37
+
Anything that doesn't match returns a `404` status code.
38
+
39
+
## Examples with `curl`
40
+
41
+
Given the following data file:
42
+
43
+
44
+
```json
45
+
{
46
+
"notes" : [
47
+
{
48
+
"id" : 1,
49
+
"title" : "Hello World"
50
+
},
51
+
{
52
+
"id" : 2,
53
+
"title" : "The second note"
54
+
}
55
+
]
56
+
}
57
+
```
58
+
59
+
To get everything:
60
+
61
+
```
62
+
curl -i localhost:8080/
63
+
```
64
+
65
+
To get the `notes` node:
66
+
67
+
68
+
```
69
+
curl -i localhost:8080/notes
70
+
```
71
+
72
+
To get the `notes/1` node:
73
+
74
+
75
+
```
76
+
curl -i localhost:8080/notes/1
77
+
```
78
+
79
+
To add a new note:
80
+
81
+
```
82
+
curl -i -X POST http://localhost:8080/notes \
83
+
-H "Content-type: application/json" \
84
+
-d '{"title": "This is another note"}'
85
+
```
86
+
87
+
To update the contents of the first note:
88
+
89
+
```
90
+
curl -i -X PUT http://localhost:3000/notes/1 \
91
+
-H "Content-type: application/json" \
92
+
-d '{"title": "This is the third note"}'
93
+
```
94
+
95
+
To delete the third note:
96
+
97
+
```
98
+
curl -i -X DELETE localhost:3000/notes/3
99
+
```
100
+
101
+
If you use a different JSON file, your paths will be different.
102
+
103
+
### Advanced Usage
104
+
105
+
To specify a different port, use the `-p` option:
106
+
107
+
```
108
+
demo-api -p 4000
109
+
```
110
+
111
+
To specify a different filename, in case you don't like `data.json` as the default, use `-f` and specify the file:
112
+
113
+
114
+
```
115
+
demo-api -f notes.json
116
+
```
117
+
118
+
To view the version, use `-v`:
119
+
120
+
```
121
+
demo-api -v
122
+
```
123
+
33
124
## Installation
34
125
35
126
To install, download the latest release to your system and copy the executable to a location on your path. Then launch it in a directory containing `data.json`.
36
127
37
128
38
129
## Roadmap
39
130
40
-
*`PUT/PATCH` support
41
-
* tests
42
-
* refactoring
131
+
* Refactoring. This code is a mess.
132
+
* A "no persist" mode - changes are accepted but not saved to the JSON file.
43
133
44
134
## Contributing
45
135
@@ -56,18 +146,20 @@ $ go get github.com/codegangsta/gin
56
146
Run development version:
57
147
58
148
```
59
-
$ gin go run app.go
149
+
$ gin --appPort 8080 go run app.go
60
150
```
61
151
62
152
The server is now listening on `localhost:3000` and will reload on code change.
0 commit comments