@@ -2,12 +2,38 @@ const express = require("express");
22const app = express ( ) ;
33const PORT = 3000 ;
44
5+ function getFileData ( response ) {
6+ // fs.readFileSync() reads a file. The first parameter is the path to the file, and the second is the encoding.
7+ try {
8+ const data = fs . readFileSync ( "locations.json" , "utf8" ) ;
9+ return JSON . parse ( data ) ;
10+ } catch ( e ) {
11+ console . error ( e ) ;
12+ response . sendStatus ( 500 ) ;
13+ return ;
14+ }
15+ }
16+
17+ function setFileData ( fileData , response ) {
18+ // fs.writeFile() writes to a file. The first parameter is the path to the file, the second is the data to write,
19+ // and the third is a callback function that takes one parameter: error.
20+ fs . writeFile ( "locations.json" , JSON . stringify ( fileData ) , ( error ) => {
21+ if ( error ) {
22+ console . error ( error ) ;
23+ response . sendStatus ( 500 ) ;
24+ return ;
25+ }
26+
27+ response . sendStatus ( 200 ) ;
28+ } ) ;
29+ }
30+
531// Creates a GET endpoint at the root URL that returns a string
632app . get ( "/" , function ( request , response ) {
7- response . send ( "Hello, world!" ) ;
33+ response . send ( "Hello, world!" ) ;
834} ) ;
935
1036// Starts the server on port 3000
11- app . listen ( PORT , function ( ) {
12- console . log ( `Server listening on port ${ PORT } !` ) ;
37+ app . listen ( PORT , function ( ) {
38+ console . log ( `Server listening on port ${ PORT } !` ) ;
1339} ) ;
0 commit comments