@@ -16,24 +16,26 @@ var express = require('express');
16
16
var bodyParser = require ( 'body-parser' ) ;
17
17
var app = express ( ) ;
18
18
19
+ var COMMENTS_FILE = path . join ( __dirname , 'comments.json' ) ;
20
+
19
21
app . set ( 'port' , ( process . env . PORT || 3000 ) ) ;
20
22
21
23
app . use ( '/' , express . static ( path . join ( __dirname , 'public' ) ) ) ;
22
24
app . use ( bodyParser . json ( ) ) ;
23
25
app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
24
26
25
27
app . get ( '/api/comments' , function ( req , res ) {
26
- fs . readFile ( 'comments.json' , function ( err , data ) {
28
+ fs . readFile ( COMMENTS_FILE , function ( err , data ) {
27
29
res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
28
30
res . json ( JSON . parse ( data ) ) ;
29
31
} ) ;
30
32
} ) ;
31
33
32
34
app . post ( '/api/comments' , function ( req , res ) {
33
- fs . readFile ( 'comments.json' , function ( err , data ) {
35
+ fs . readFile ( COMMENTS_FILE , function ( err , data ) {
34
36
var comments = JSON . parse ( data ) ;
35
37
comments . push ( req . body ) ;
36
- fs . writeFile ( 'comments.json' , JSON . stringify ( comments , null , 4 ) , function ( err ) {
38
+ fs . writeFile ( COMMENTS_FILE , JSON . stringify ( comments , null , 4 ) , function ( err ) {
37
39
res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
38
40
res . json ( comments ) ;
39
41
} ) ;
0 commit comments