Skip to content

Commit f5ab906

Browse files
committed
Update v1.0.1
- change index page UI - use 'toLowerCase' on api path string - search json file with postfix '.json' for getting api list
1 parent b49a0d0 commit f5ab906

20 files changed

+169
-49
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ $ NODE_ENV=production npm start
4343
```
4444

4545
## RESTful API
46-
- GET
47-
- DELETE
48-
- POST : BODY - raw data, application/json
46+
- GET : application/json, postfix '.json'
47+
- DELETE : application/json, postfix '.json'
48+
- POST : application/json, postfix '.json', BODY - raw data
4949
![Image](./public/readmeImage/example_post_body.png)
50+
- The postfix of url must be '.json' for using a json file
51+
- If use postfix '.json', 'Content-Type' of the response header will be set to 'application/json'
5052

5153

5254
## #set env....

app.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ app.use(express.static(path.join(__dirname, 'public')));
2626
app.use(function(req, res, next) {
2727
if (req.path.endsWith('.json')) {
2828
res.setHeader('Content-Type', 'application/json');
29-
} else {
30-
res.setHeader('Content-Type', 'text/html');
3129
}
3230
next();
3331
});
@@ -37,20 +35,29 @@ app.use('/*.json', jsonFile);
3735

3836
// catch 404 and forward to error handler
3937
app.use(function(req, res, next) {
40-
var err = new Error('Not Found');
38+
var errorMessage = 'Not Found';
39+
var contentType = req.headers['content-type'];
40+
41+
if (contentType == 'application/json' && path.extname(req.path) != '.json') {
42+
errorMessage += ' : the postfix of url must be \'.json\' for using a json file';
43+
}
44+
45+
var err = new Error(errorMessage);
4146
err.status = 404;
4247
next(err);
4348
});
4449

4550
// error handler
4651
app.use(function(err, req, res, next) {
52+
var contentType = req.headers['content-type'];
53+
console.log(contentType);
4754
// set locals, only providing error in development
4855
res.locals.message = err.message;
4956
res.locals.error = req.app.get('env') === 'development' ? err : {};
5057

5158
// render the error page
5259
res.status(err.status || 500);
53-
if (req.path.endsWith('.json')) {
60+
if (path.extname(req.path) == '.json' || contentType == 'application/json') {
5461
// .json error result
5562
res.json(err.message);
5663
} else {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonapitestserver",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"private": true,
55
"scripts": {
66
"start": "node ./bin/www"

0 commit comments

Comments
 (0)