@@ -16,24 +16,26 @@ var express = require('express');
1616var bodyParser = require ( 'body-parser' ) ;
1717var app = express ( ) ;
1818
19+ var COMMENTS_FILE = path . join ( __dirname , 'comments.json' ) ;
20+
1921app . set ( 'port' , ( process . env . PORT || 3000 ) ) ;
2022
2123app . use ( '/' , express . static ( path . join ( __dirname , 'public' ) ) ) ;
2224app . use ( bodyParser . json ( ) ) ;
2325app . use ( bodyParser . urlencoded ( { extended : true } ) ) ;
2426
2527app . get ( '/api/comments' , function ( req , res ) {
26- fs . readFile ( 'comments.json' , function ( err , data ) {
28+ fs . readFile ( COMMENTS_FILE , function ( err , data ) {
2729 res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
2830 res . json ( JSON . parse ( data ) ) ;
2931 } ) ;
3032} ) ;
3133
3234app . post ( '/api/comments' , function ( req , res ) {
33- fs . readFile ( 'comments.json' , function ( err , data ) {
35+ fs . readFile ( COMMENTS_FILE , function ( err , data ) {
3436 var comments = JSON . parse ( data ) ;
3537 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 ) {
3739 res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
3840 res . json ( comments ) ;
3941 } ) ;
0 commit comments