Skip to content

Commit f41ff1d

Browse files
authored
Merge pull request #3 from tigi44/restful_branch
Restful branch
2 parents 53b1a61 + 7c7b490 commit f41ff1d

File tree

3 files changed

+86
-24
lines changed

3 files changed

+86
-24
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@ $ NODE_ENV=production npm start
4646
- GET : Read (Content-Type -> application/json)
4747
- DELETE : Delete (Content-Type -> application/json)
4848
- POST : Create , Read (Content-Type -> application/json, BODY - raw data)
49-
- PUT : Create , Update (Content-Type -> application/json, BODY - raw data)
49+
- PUT : Update, Create (Content-Type -> application/json, BODY - raw data)
5050
![Image](./public/readmeImage/example_post_body.png)
5151
- if you add postfix '.json' to api url, the 'Content-Type' will be changed to 'application/json'
5252

53+
## Read API PATH
54+
```
55+
http://localhost:3000/test --> find `test.json` file
56+
http://localhost:3000/test.json --> find `test.json` file
57+
http://localhost:3000/test/ --> find a file list in the `test` directory
58+
```
59+
5360
## #set env....
5461
```
5562
$ export NODE_ENV=development

routes/jsonFile.js

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ var path = require('path');
55
var url = require('url');
66
var ff = require('../routes/findFile');
77

8-
// get
8+
// get : read
99
router.get('/', function(req, res, next) {
1010
var resultData;
1111
var reqPath = getFilePathByRequest(req);
1212
var filePath = addExtNameJson(reqPath);
1313

1414
try {
15-
if (fs.existsSync(reqPath) && fs.lstatSync(reqPath).isDirectory()) {
15+
if (isExistFile(filePath)) {
16+
resultData = readData(filePath);
17+
} else if (isDirectory(reqPath)) {
1618
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);
2019
} else {
2120
next();
2221
}
@@ -31,21 +30,19 @@ router.get('/', function(req, res, next) {
3130
res.json(resultData);
3231
});
3332

34-
// post
33+
// post : create | read
3534
router.post('/', function(req, res, next) {
3635
var resultData;
3736
var json = req.body;
3837
var filePath = getFilePathByRequest(req);
3938
filePath = addExtNameJson(filePath);
4039

4140
try {
42-
if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
43-
var fileString = fs.readFileSync(filePath, 'utf8');
44-
resultData = JSON.parse(fileString);
41+
if (isExistFile(filePath)) {
42+
resultData = readData(filePath);
4543
} else {
46-
mkdirp(filePath);
47-
fs.writeFileSync(filePath, JSON.stringify(json), 'utf8');
48-
resultData = json;
44+
resultData = createData(filePath, json);
45+
res.status(201);
4946
}
5047
} catch(e) {
5148
// console.log(e);
@@ -54,49 +51,61 @@ router.post('/', function(req, res, next) {
5451
err.status = 500;
5552
next(err);
5653
}
54+
5755
res.json(resultData);
5856
});
5957

60-
// put
58+
// put : update | create
6159
router.put('/', function(req, res, next) {
6260
var resultData;
6361
var json = req.body;
6462
var filePath = getFilePathByRequest(req);
6563
filePath = addExtNameJson(filePath);
6664

6765
try {
68-
mkdirp(filePath);
69-
fs.writeFileSync(filePath, JSON.stringify(json), 'utf8');
70-
resultData = json;
66+
if (isExistFile(filePath)) {
67+
resultData = updateData(filePath, json);
68+
} else {
69+
resultData = createData(filePath, json);
70+
res.status(201);
71+
}
7172
} catch(e) {
7273
// console.log(e);
7374
var errorMessage = 'Fail Put';
7475
var err = new Error(errorMessage);
7576
err.status = 500;
7677
next(err);
7778
}
79+
7880
res.json(resultData);
7981
});
8082

81-
// delete
83+
// delete : delete
8284
router.delete('/', function(req, res, next) {
8385
var resultData;
8486
var filePath = getFilePathByRequest(req);
8587
filePath = addExtNameJson(filePath);
8688

8789
try {
88-
fs.unlinkSync(filePath);
89-
resultData = 'Success Delete';
90+
if (isExistFile(filePath)) {
91+
resultData = deleteData(filePath);
92+
} else {
93+
res.status(204);
94+
}
9095
} catch(e) {
9196
// console.log(e);
9297
var errorMessage = 'Fail Delete';
9398
var err = new Error(errorMessage);
9499
err.status = 500;
95100
next(err);
96101
}
102+
97103
res.json(resultData);
98104
});
99105

106+
107+
// private function
108+
100109
function addExtNameJson(urlPath) {
101110
var resultPath;
102111
var extname = path.extname(urlPath);
@@ -108,7 +117,7 @@ function addExtNameJson(urlPath) {
108117
resultPath = urlPath.replace(extname, ff.extJson);
109118
}
110119
} else {
111-
resultPath = urlPath + ff.extJson;
120+
resultPath = urlPath.endsWith(ff.extJson) ? urlPath : urlPath + ff.extJson;
112121
}
113122

114123
return resultPath;
@@ -123,10 +132,56 @@ function getFilePathByRequest(req) {
123132
function mkdirp(filePath) {
124133
var dirname = path.dirname(filePath);
125134
if (fs.existsSync(dirname)) {
126-
return true;
135+
return false;
127136
}
137+
128138
mkdirp(dirname);
129139
fs.mkdirSync(dirname);
140+
141+
return true;
142+
}
143+
144+
function isDirectory(reqPath) {
145+
return fs.existsSync(reqPath) && fs.lstatSync(reqPath).isDirectory();
146+
}
147+
148+
function isExistFile(filePath) {
149+
return fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();
150+
}
151+
152+
153+
// function : create, read, update, delete
154+
155+
function createData(filePath, json) {
156+
let resultData;
157+
158+
mkdirp(filePath);
159+
fs.writeFileSync(filePath, JSON.stringify(json), 'utf8');
160+
resultData = json;
161+
162+
return resultData;
163+
}
164+
165+
function readData(filePath) {
166+
let resultData;
167+
168+
let fileString = fs.readFileSync(filePath, 'utf8');
169+
resultData = JSON.parse(fileString);
170+
171+
return resultData;
172+
}
173+
174+
function updateData(filePath, json) {
175+
return createData(filePath, json);
176+
}
177+
178+
function deleteData(filePath) {
179+
let resultData;
180+
181+
fs.unlinkSync(filePath);
182+
resultData = 'Success Delete';
183+
184+
return resultData;
130185
}
131186

132187
module.exports = router;

views/index.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@
192192
function requestXhttp(url, method, data, successCallback, errorCallback) {
193193
var xhttp = new XMLHttpRequest();
194194
xhttp.onreadystatechange = function() {
195-
if (this.readyState == 4 && this.status == 200) {
195+
if (this.readyState == 4 && (200 <= this.status && this.status < 300)) {
196196
if (successCallback) {
197197
successCallback(this.responseText);
198198
}
199-
} else if (this.readyState == 4 && this.status != 200) {
199+
} else if (this.readyState == 4 && !(200 <= this.status && this.status < 300)) {
200200
if (errorCallback) {
201201
errorCallback(this.responseText);
202202
} else {

0 commit comments

Comments
 (0)