From d3fe98bdb95705aab2ab98904447a2cb5d952b1f Mon Sep 17 00:00:00 2001 From: tigi44 Date: Mon, 1 Apr 2019 15:42:00 +0900 Subject: [PATCH 1/3] Update restful api --- README.md | 2 +- routes/jsonFile.js | 95 ++++++++++++++++++++++++++++++++++++---------- views/index.ejs | 4 +- 3 files changed, 78 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 6452e8f..5af5d76 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ $ NODE_ENV=production npm start - GET : Read (Content-Type -> application/json) - DELETE : Delete (Content-Type -> application/json) - POST : Create , Read (Content-Type -> application/json, BODY - raw data) -- PUT : Create , Update (Content-Type -> application/json, BODY - raw data) +- PUT : Update, Create (Content-Type -> application/json, BODY - raw data) ![Image](./public/readmeImage/example_post_body.png) - if you add postfix '.json' to api url, the 'Content-Type' will be changed to 'application/json' diff --git a/routes/jsonFile.js b/routes/jsonFile.js index dfc9a81..24bc660 100644 --- a/routes/jsonFile.js +++ b/routes/jsonFile.js @@ -5,18 +5,17 @@ var path = require('path'); var url = require('url'); var ff = require('../routes/findFile'); -// get +// get : read router.get('/', function(req, res, next) { var resultData; var reqPath = getFilePathByRequest(req); var filePath = addExtNameJson(reqPath); try { - if (fs.existsSync(reqPath) && fs.lstatSync(reqPath).isDirectory()) { + if (isDirectory(reqPath)) { resultData = ff.hierarchyFiles(ff.findFiles(reqPath)); - } else if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) { - var fileString = fs.readFileSync(filePath, 'utf8'); - resultData = JSON.parse(fileString); + } else if (isExistFile(filePath)) { + resultData = readData(filePath); } else { next(); } @@ -31,7 +30,7 @@ router.get('/', function(req, res, next) { res.json(resultData); }); -// post +// post : create | read router.post('/', function(req, res, next) { var resultData; var json = req.body; @@ -39,13 +38,11 @@ router.post('/', function(req, res, next) { filePath = addExtNameJson(filePath); try { - if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) { - var fileString = fs.readFileSync(filePath, 'utf8'); - resultData = JSON.parse(fileString); + if (isExistFile(filePath)) { + resultData = readData(filePath); } else { - mkdirp(filePath); - fs.writeFileSync(filePath, JSON.stringify(json), 'utf8'); - resultData = json; + resultData = createData(filePath, json); + res.status(201); } } catch(e) { // console.log(e); @@ -54,10 +51,11 @@ router.post('/', function(req, res, next) { err.status = 500; next(err); } + res.json(resultData); }); -// put +// put : update | create router.put('/', function(req, res, next) { var resultData; var json = req.body; @@ -65,9 +63,12 @@ router.put('/', function(req, res, next) { filePath = addExtNameJson(filePath); try { - mkdirp(filePath); - fs.writeFileSync(filePath, JSON.stringify(json), 'utf8'); - resultData = json; + if (isExistFile(filePath)) { + resultData = updateData(filePath, json); + } else { + resultData = createData(filePath, json); + res.status(201); + } } catch(e) { // console.log(e); var errorMessage = 'Fail Put'; @@ -75,18 +76,22 @@ router.put('/', function(req, res, next) { err.status = 500; next(err); } + res.json(resultData); }); -// delete +// delete : delete router.delete('/', function(req, res, next) { var resultData; var filePath = getFilePathByRequest(req); filePath = addExtNameJson(filePath); try { - fs.unlinkSync(filePath); - resultData = 'Success Delete'; + if (isExistFile(filePath)) { + resultData = deleteData(filePath); + } else { + res.status(204); + } } catch(e) { // console.log(e); var errorMessage = 'Fail Delete'; @@ -94,9 +99,13 @@ router.delete('/', function(req, res, next) { err.status = 500; next(err); } + res.json(resultData); }); + +// private function + function addExtNameJson(urlPath) { var resultPath; var extname = path.extname(urlPath); @@ -123,10 +132,56 @@ function getFilePathByRequest(req) { function mkdirp(filePath) { var dirname = path.dirname(filePath); if (fs.existsSync(dirname)) { - return true; + return false; } + mkdirp(dirname); fs.mkdirSync(dirname); + + return true; +} + +function isDirectory(reqPath) { + return fs.existsSync(reqPath) && fs.lstatSync(reqPath).isDirectory(); +} + +function isExistFile(filePath) { + return fs.existsSync(filePath) && fs.lstatSync(filePath).isFile(); +} + + +// function : create, read, update, delete + +function createData(filePath, json) { + let resultData; + + mkdirp(filePath); + fs.writeFileSync(filePath, JSON.stringify(json), 'utf8'); + resultData = json; + + return resultData; +} + +function readData(filePath) { + let resultData; + + let fileString = fs.readFileSync(filePath, 'utf8'); + resultData = JSON.parse(fileString); + + return resultData; +} + +function updateData(filePath, json) { + return createData(filePath, json); +} + +function deleteData(filePath) { + let resultData; + + fs.unlinkSync(filePath); + resultData = 'Success Delete'; + + return resultData; } module.exports = router; diff --git a/views/index.ejs b/views/index.ejs index b5c2238..ed0cb17 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -192,11 +192,11 @@ function requestXhttp(url, method, data, successCallback, errorCallback) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { + if (this.readyState == 4 && (200 <= this.status && this.status < 300)) { if (successCallback) { successCallback(this.responseText); } - } else if (this.readyState == 4 && this.status != 200) { + } else if (this.readyState == 4 && !(200 <= this.status && this.status < 300)) { if (errorCallback) { errorCallback(this.responseText); } else { From 2f7cba8454b83f6fea76ddcc105a8dca5280458c Mon Sep 17 00:00:00 2001 From: tigi44 Date: Mon, 1 Apr 2019 15:54:35 +0900 Subject: [PATCH 2/3] Update path list api --- routes/jsonFile.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routes/jsonFile.js b/routes/jsonFile.js index 24bc660..65b912f 100644 --- a/routes/jsonFile.js +++ b/routes/jsonFile.js @@ -12,10 +12,10 @@ router.get('/', function(req, res, next) { var filePath = addExtNameJson(reqPath); try { - if (isDirectory(reqPath)) { - resultData = ff.hierarchyFiles(ff.findFiles(reqPath)); - } else if (isExistFile(filePath)) { + if (isExistFile(filePath)) { resultData = readData(filePath); + } else if (isDirectory(reqPath)) { + resultData = ff.hierarchyFiles(ff.findFiles(reqPath)); } else { next(); } @@ -117,7 +117,7 @@ function addExtNameJson(urlPath) { resultPath = urlPath.replace(extname, ff.extJson); } } else { - resultPath = urlPath + ff.extJson; + resultPath = urlPath.endsWith(ff.extJson) ? urlPath : urlPath + ff.extJson; } return resultPath; From 7c7b490a454652ea5c77ba026e0e14e8d6917e4c Mon Sep 17 00:00:00 2001 From: tigi44 Date: Mon, 1 Apr 2019 16:04:59 +0900 Subject: [PATCH 3/3] Update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 5af5d76..ad5b26c 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,13 @@ $ NODE_ENV=production npm start ![Image](./public/readmeImage/example_post_body.png) - if you add postfix '.json' to api url, the 'Content-Type' will be changed to 'application/json' +## Read API PATH +``` +http://localhost:3000/test --> find `test.json` file +http://localhost:3000/test.json --> find `test.json` file +http://localhost:3000/test/ --> find a file list in the `test` directory +``` + ## #set env.... ``` $ export NODE_ENV=development