@@ -15,12 +15,13 @@ app.use(cors())
15
15
app . post ( '/:name.json' , async ( request , response ) => {
16
16
try {
17
17
const filename = path . join ( infoDir , `${ request . params . name } .json` )
18
+ await fs . ensureDir ( infoDir )
18
19
await fs . writeFile ( filename , JSON . stringify ( request . body , null , 2 ) )
19
20
response . statusCode = 200
20
- response . json ( { ok : true } )
21
+ response . json ( { ok : true } )
21
22
} catch ( error ) {
22
23
response . statusCode = 500
23
- response . json ( { error : "An error occurred" } )
24
+ response . json ( { error : "An error occurred" } )
24
25
console . error ( error )
25
26
}
26
27
@@ -29,6 +30,7 @@ app.post('/:name.json', async (request, response) => {
29
30
30
31
app . get ( '/_all.json' , async ( request , response ) => {
31
32
try {
33
+ await fs . ensureDir ( infoDir )
32
34
const files = await fs . readdir ( infoDir )
33
35
const promises = files . map ( file => fs . readFile ( path . join ( infoDir , file ) , 'utf8' ) . then ( raw => [ file , raw ] ) )
34
36
const raws = await Promise . all ( promises )
@@ -40,7 +42,7 @@ app.get('/_all.json', async (request, response) => {
40
42
response . json ( result )
41
43
} catch ( error ) {
42
44
response . statusCode = 500
43
- response . json ( { error : "An error occurred" } )
45
+ response . json ( { error : "An error occurred" } )
44
46
console . error ( error )
45
47
}
46
48
response . end ( )
@@ -49,15 +51,16 @@ app.get('/_all.json', async (request, response) => {
49
51
app . get ( '/:name.json' , async ( request , response ) => {
50
52
try {
51
53
const filename = path . join ( infoDir , `${ request . params . name } .json` )
54
+ await fs . ensureDir ( infoDir )
52
55
const raw = await fs . readFile ( filename , 'utf8' )
53
56
response . json ( JSON . parse ( raw ) )
54
57
} catch ( error ) {
55
58
if ( error . code === 'ENOENT' ) {
56
59
response . statusCode = 404
57
- response . json ( { error : "File not found" } )
60
+ response . json ( { error : "File not found" } )
58
61
} else {
59
62
response . statusCode = 500
60
- response . json ( { error : "An error occurred" } )
63
+ response . json ( { error : "An error occurred" } )
61
64
console . error ( error )
62
65
}
63
66
}
@@ -67,34 +70,35 @@ app.get('/:name.json', async (request, response) => {
67
70
app . delete ( '/_all.json' , async ( request , response ) => {
68
71
try {
69
72
await fs . emptyDir ( infoDir )
70
- response . json ( { ok : true } )
73
+ response . json ( { ok : true } )
71
74
} catch ( error ) {
72
75
response . statusCode = 500
73
- response . json ( { error : "An error occurred" } )
76
+ response . json ( { error : "An error occurred" } )
74
77
console . error ( error )
75
78
}
76
79
response . end ( )
77
80
} )
78
81
79
82
app . delete ( '/:name.json' , async ( request , response ) => {
80
83
try {
84
+ await fs . ensureDir ( infoDir )
81
85
await fs . unlink ( path . join ( infoDir , `${ request . params . name } .json` ) )
82
- response . json ( { ok : true } )
86
+ response . json ( { ok : true } )
83
87
} catch ( error ) {
84
88
if ( error . code !== 'ENOENT' ) {
85
89
response . statusCode = 500
86
- response . json ( { error : "An error occurred" } )
90
+ response . json ( { error : "An error occurred" } )
87
91
console . error ( error )
88
92
} else {
89
- response . json ( { ok : true } )
93
+ response . json ( { ok : true } )
90
94
}
91
95
}
92
96
response . end ( )
93
97
} )
94
98
95
99
app . use ( ( request , response ) => {
96
100
response . statusCode = 404
97
- response . json ( { error : "Not found" } )
101
+ response . json ( { error : "Not found" } )
98
102
response . end ( )
99
103
} )
100
104
0 commit comments