@@ -3,30 +3,42 @@ var router = express.Router();
33var fs = require ( 'fs' ) ;
44var path = require ( 'path' ) ;
55var url = require ( 'url' ) ;
6+ var ff = require ( '../routes/findFile' ) ;
67
78// get
89router . get ( '/' , function ( req , res , next ) {
910 var resultData ;
10- var filepath = getFilePathByRequest ( req ) ;
11+ var reqPath = getFilePathByRequest ( req ) ;
12+ var filePath = addExtNameJson ( reqPath ) ;
1113
1214 try {
13- // filepath로 해당 파일을 찾은 경우 json으로 출력
14- var fileString = fs . readFileSync ( filepath , 'utf8' ) ;
15- resultData = JSON . parse ( fileString ) ;
15+ if ( fs . existsSync ( reqPath ) && fs . lstatSync ( reqPath ) . isDirectory ( ) ) {
16+ resultData = ff . hierarchyFiles ( ff . findFiles ( reqPath ) ) ;
17+ } else if ( fs . existsSync ( filePath ) && fs . lstatSync ( filePath ) . isFile ( ) ) {
18+ var fileString = fs . readFileSync ( filePath , 'utf8' ) ;
19+ resultData = JSON . parse ( fileString ) ;
20+ } else {
21+ next ( ) ;
22+ }
1623 } catch ( e ) {
1724 // console.log(e);
18- next ( ) ;
25+ var errorMessage = 'Fail Get' ;
26+ var err = new Error ( errorMessage ) ;
27+ err . status = 500 ;
28+ next ( err ) ;
1929 }
30+
2031 res . json ( resultData ) ;
2132} ) ;
2233
2334// delete
2435router . delete ( '/' , function ( req , res , next ) {
2536 var resultData ;
26- var filepath = getFilePathByRequest ( req ) ;
37+ var filePath = getFilePathByRequest ( req ) ;
38+ filePath = addExtNameJson ( filePath ) ;
2739
2840 try {
29- fs . unlinkSync ( filepath ) ;
41+ fs . unlinkSync ( filePath ) ;
3042 resultData = 'Success Delete' ;
3143 } catch ( e ) {
3244 // console.log(e);
@@ -42,11 +54,12 @@ router.delete('/', function(req, res, next) {
4254router . post ( '/' , function ( req , res , next ) {
4355 var resultData ;
4456 var json = req . body ;
45- var filepath = getFilePathByRequest ( req ) ;
57+ var filePath = getFilePathByRequest ( req ) ;
58+ filePath = addExtNameJson ( filePath ) ;
4659
4760 try {
48- mkdirp ( filepath ) ;
49- fs . writeFileSync ( filepath , JSON . stringify ( json ) , 'utf8' ) ;
61+ mkdirp ( filePath ) ;
62+ fs . writeFileSync ( filePath , JSON . stringify ( json ) , 'utf8' ) ;
5063 resultData = json ;
5164 } catch ( e ) {
5265 // console.log(e);
@@ -60,31 +73,29 @@ router.post('/', function(req, res, next) {
6073
6174function addExtNameJson ( urlPath ) {
6275 var resultPath ;
63- var extJson = '.json' ;
6476 var extname = path . extname ( urlPath ) ;
6577
6678 if ( extname ) {
67- if ( extname == extJson ) {
79+ if ( extname == ff . extJson ) {
6880 resultPath = urlPath ;
6981 } else {
70- resultPath = urlPath . replace ( extname , extJson ) ;
82+ resultPath = urlPath . replace ( extname , ff . extJson ) ;
7183 }
7284 } else {
73- resultPath = urlPath + extJson ;
85+ resultPath = urlPath + ff . extJson ;
7486 }
7587
7688 return resultPath ;
7789}
7890
7991function getFilePathByRequest ( req ) {
8092 var urlPath = url . parse ( req . originalUrl ) . pathname ;
81- urlPath = addExtNameJson ( urlPath ) ;
8293
8394 return path . join ( './jsonFile' , urlPath . toLowerCase ( ) ) ;
8495}
8596
86- function mkdirp ( filepath ) {
87- var dirname = path . dirname ( filepath ) ;
97+ function mkdirp ( filePath ) {
98+ var dirname = path . dirname ( filePath ) ;
8899 if ( fs . existsSync ( dirname ) ) {
89100 return true ;
90101 }
0 commit comments