Skip to content

Commit

Permalink
Edit post method : "Content-type", "application/json"
Browse files Browse the repository at this point in the history
v0.0.2
  • Loading branch information
tigi44 committed Mar 18, 2018
1 parent 1f93e41 commit 04d4697
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 43 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# JsonApiTestServer
[![Build Status](https://travis-ci.org/tigi44/JsonApiTestServer.svg?branch=master)](https://travis-ci.org/tigi44/JsonApiTestServer)

## FEATURE
- [NodeJS](https://nodejs.org)
- [express](https://www.npmjs.com/package/express) module

## NodeJS
- install NodeJS : Latest Current Version version
- this project using v9.8.0
Expand Down Expand Up @@ -30,20 +34,29 @@ $ PORT=8080 npm start
```
$ DEBUG=jsonapitestserver:* npm start
```
- node env
```
$ NODE_ENV=development npm start
$ NODE_ENV=production npm start
```

## FEATURE
- NodeJS
- express module

### RESTful API
- GET : application/json
- POST : x-www-form-urlencoded `json : jsonString....`
## RESTful API
- GET
- DELETE
- POST : BODY - raw data, application/json
![Image](./public/readmeImage/example_post_body.png)


## set env....
## #set env....
```
$ export NODE_ENV=development
$ export NODE_ENV=production
```
```
$ export NODE_ENV=production
$ export DEBUG=jsonapitestserver:*
```

## #package
### nodemon
- auto restart nodejs package
- https://www.npmjs.com/package/nodemon
18 changes: 17 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// set response header
app.use(function(req, res, next) {
if (req.path.endsWith('.json')) {
res.setHeader('Content-Type', 'application/json');
} else {
res.setHeader('Content-Type', 'text/html');
}
next();
});

app.use('/', index);
app.use('/*.json', jsonFile);

Expand All @@ -40,7 +50,13 @@ app.use(function(err, req, res, next) {

// render the error page
res.status(err.status || 500);
res.render('error');
if (req.path.endsWith('.json')) {
// .json error result
res.json(err.message);
} else {
// error page render
res.render('error');
}
});

module.exports = app;
7 changes: 1 addition & 6 deletions jsonFile/test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
{
"string" : "string",
"number" : 1,
"null" : null,
"Boolean" : true
}
{"string":"string","number":1,"null":null,"Boolean":true}
Binary file added public/readmeImage/example_post_body.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 13 additions & 23 deletions routes/jsonFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ var express = require('express');
var router = express.Router();
var fs = require('fs');
var path = require('path');
var url = require('url');

// get
router.get('/', function(req, res, next) {
res.setHeader('Content-Type', 'application/json');

var resultData;
var filepath = req.originalUrl;
filepath = path.join('./jsonFile', filepath);
var filepath = getFilePathByRequest(req);

try {
// filepath로 해당 파일을 찾은 경우 json으로 출력
Expand All @@ -23,12 +22,10 @@ router.get('/', function(req, res, next) {
res.json(resultData);
});

// delete
router.delete('/', function(req, res, next) {
res.setHeader('Content-Type', 'application/json');

var resultData;
var filepath = req.originalUrl;
filepath = path.join('./jsonFile', filepath);
var filepath = getFilePathByRequest(req);

try {
fs.unlinkSync(filepath);
Expand All @@ -41,23 +38,11 @@ router.delete('/', function(req, res, next) {
res.json(resultData);
});

// post
router.post('/', function(req, res, next) {
res.setHeader('Content-Type', 'application/json');

var resultData;
var json;
var jsonData = req.body.json;
var filepath = req.originalUrl;
filepath = path.join('./jsonFile', filepath);

try {
json = JSON.parse(jsonData);
} catch(e) {
// console.log(e);
res.status(500);
res.json(e.message);
return;
}
var json = req.body;
var filepath = getFilePathByRequest(req);

try {
mkdirp(filepath);
Expand All @@ -71,6 +56,11 @@ router.post('/', function(req, res, next) {
res.json(resultData);
});

function getFilePathByRequest(req) {
var urlPath = url.parse(req.originalUrl).pathname;
return path.join('./jsonFile', urlPath);
}

function mkdirp(filepath) {
var dirname = path.dirname(filepath);
if (fs.existsSync(dirname)) {
Expand Down
6 changes: 2 additions & 4 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
// console.log(requestUrl);
var jsonString = _textareaJsonEl.value;
requestXhttp(requestUrl, "POST", 'json=' + encodeURIComponent(jsonString),
requestXhttp(requestUrl, "POST", jsonString,
function(data) {
location.reload();
},
Expand Down Expand Up @@ -116,9 +116,7 @@
}
};
xhttp.open(method, url, true);
if (method == 'POST') {
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send(data);
}
</script>
Expand Down

0 comments on commit 04d4697

Please sign in to comment.