@@ -15,12 +15,13 @@ app.use(cors())
1515app . post ( '/:name.json' , async ( request , response ) => {
1616 try {
1717 const filename = path . join ( infoDir , `${ request . params . name } .json` )
18+ await fs . ensureDir ( infoDir )
1819 await fs . writeFile ( filename , JSON . stringify ( request . body , null , 2 ) )
1920 response . statusCode = 200
20- response . json ( { ok : true } )
21+ response . json ( { ok : true } )
2122 } catch ( error ) {
2223 response . statusCode = 500
23- response . json ( { error : "An error occurred" } )
24+ response . json ( { error : "An error occurred" } )
2425 console . error ( error )
2526 }
2627
@@ -29,6 +30,7 @@ app.post('/:name.json', async (request, response) => {
2930
3031app . get ( '/_all.json' , async ( request , response ) => {
3132 try {
33+ await fs . ensureDir ( infoDir )
3234 const files = await fs . readdir ( infoDir )
3335 const promises = files . map ( file => fs . readFile ( path . join ( infoDir , file ) , 'utf8' ) . then ( raw => [ file , raw ] ) )
3436 const raws = await Promise . all ( promises )
@@ -40,7 +42,7 @@ app.get('/_all.json', async (request, response) => {
4042 response . json ( result )
4143 } catch ( error ) {
4244 response . statusCode = 500
43- response . json ( { error : "An error occurred" } )
45+ response . json ( { error : "An error occurred" } )
4446 console . error ( error )
4547 }
4648 response . end ( )
@@ -49,15 +51,16 @@ app.get('/_all.json', async (request, response) => {
4951app . get ( '/:name.json' , async ( request , response ) => {
5052 try {
5153 const filename = path . join ( infoDir , `${ request . params . name } .json` )
54+ await fs . ensureDir ( infoDir )
5255 const raw = await fs . readFile ( filename , 'utf8' )
5356 response . json ( JSON . parse ( raw ) )
5457 } catch ( error ) {
5558 if ( error . code === 'ENOENT' ) {
5659 response . statusCode = 404
57- response . json ( { error : "File not found" } )
60+ response . json ( { error : "File not found" } )
5861 } else {
5962 response . statusCode = 500
60- response . json ( { error : "An error occurred" } )
63+ response . json ( { error : "An error occurred" } )
6164 console . error ( error )
6265 }
6366 }
@@ -67,34 +70,35 @@ app.get('/:name.json', async (request, response) => {
6770app . delete ( '/_all.json' , async ( request , response ) => {
6871 try {
6972 await fs . emptyDir ( infoDir )
70- response . json ( { ok : true } )
73+ response . json ( { ok : true } )
7174 } catch ( error ) {
7275 response . statusCode = 500
73- response . json ( { error : "An error occurred" } )
76+ response . json ( { error : "An error occurred" } )
7477 console . error ( error )
7578 }
7679 response . end ( )
7780} )
7881
7982app . delete ( '/:name.json' , async ( request , response ) => {
8083 try {
84+ await fs . ensureDir ( infoDir )
8185 await fs . unlink ( path . join ( infoDir , `${ request . params . name } .json` ) )
82- response . json ( { ok : true } )
86+ response . json ( { ok : true } )
8387 } catch ( error ) {
8488 if ( error . code !== 'ENOENT' ) {
8589 response . statusCode = 500
86- response . json ( { error : "An error occurred" } )
90+ response . json ( { error : "An error occurred" } )
8791 console . error ( error )
8892 } else {
89- response . json ( { ok : true } )
93+ response . json ( { ok : true } )
9094 }
9195 }
9296 response . end ( )
9397} )
9498
9599app . use ( ( request , response ) => {
96100 response . statusCode = 404
97- response . json ( { error : "Not found" } )
101+ response . json ( { error : "Not found" } )
98102 response . end ( )
99103} )
100104
0 commit comments