ãã®èšäºã¯ããã®ç¡å®ã®ã¿ã€ãã«ã«ãããããããStackoverflowã«é¢ããéåžžã«åé·ãªè°è«ãåŒãèµ·ããããããç¡èŠããããšã¯ã§ããŸããã§ãããåºå€§ããææ¡ããè©Šã¿ïŒREST APIã®æèœãªèšèšã«ã€ããŠæ確ã«äŒããããïŒã¯ãæããã«ãèè ã¯å€ãã®æ¹æ³ã§æåããŸããããå®å šã§ã¯ãããŸããã§ããããããã«ãããç§ãã¡ã¯ããšã¯ã¹ãã¬ã¹ãã¡ã³ã®è»éã«å ãããšããäºå®ã ãã§ãªããè°è«ã®çšåºŠã«ãããŠãªãªãžãã«ãšç«¶äºããããšæã£ãŠããŸãã
èªæžã楜ããïŒ
REST APIã¯ãçŸåšå©çšå¯èœãªæãäžè¬çãªã¿ã€ãã®WebãµãŒãã¹ã®1ã€ã§ãã圌ãã®å©ããåããŠããã©ãŠã¶ã¢ããªã±ãŒã·ã§ã³ãå«ãããŸããŸãªã¯ã©ã€ã¢ã³ãã¯ãRESTAPIãä»ããŠãµãŒããŒãšæ å ±ã亀æã§ããŸãã
ãããã£ãŠãéäžã§åé¡ãçºçããªãããã«ãRESTAPIãæ£ããèšèšããããšãéåžžã«éèŠã§ããæ¶è²»è ã®èŠ³ç¹ãããAPIã®ã»ãã¥ãªãã£ãããã©ãŒãã³ã¹ãããã³äœ¿ãããããæ€èšããŠãã ããã
ããããªããšãAPIã䜿çšããŠããã客æ§ã«åé¡ãçºçããŸããããã¯èç«ããããŠç ©ãããããšã§ããäžè¬çãªèŠåã«åŸããªãå Žåãã¢ãŒããã¯ãã£ã¯èª°ããæåŸ ãããã®ãšã¯ç°ãªããããAPIãç¶æãã人ãšé¡§å®¢ãæ··ä¹±ãããã ãã§ãã
ãã®èšäºã§ã¯ãREST APIã䜿çšãããã¹ãŠã®äººã«ãšã£ãŠã·ã³ãã«ã§ç解ããããããã«ãRESTAPIãèšèšããæ¹æ³ã«ã€ããŠèª¬æããŸãããã®ãããªAPIãä»ããŠã¯ã©ã€ã¢ã³ãã«éä¿¡ãããããŒã¿ã¯æ©å¯æ å ±ã§ããå¯èœæ§ããããããèä¹ æ§ãã»ãã¥ãªãã£ãããã³é床ã確ä¿ããŸãã
ãããã¯ãŒã¯ã¢ããªã±ãŒã·ã§ã³ã倱æããçç±ãšãªãã·ã§ã³ã¯å€æ°ãããããREST APIã®ãšã©ãŒãé©åã«åŠçãããæ¶è²»è ãåé¡ã«å¯ŸåŠã§ããããã«æšæºã®HTTPã³ãŒããä»éããŠããããšã確èªããå¿ èŠããããŸãã
JSONãåãå ¥ããå¿çãšããŠJSONãè¿ããŸã
REST APIã¯ãèŠæ±ãã€ããŒãã®JSONãåãå ¥ããJSONå¿çãéä¿¡ããå¿ èŠããããŸãã JSONã¯ããŒã¿è»¢éæšæºã§ããã»ãšãã©ãã¹ãŠã®ãããã¯ãŒã¯ãã¯ãããžãŒãããã䜿çšããããã«é©åãããŠããŸããJavaScriptã«ã¯ãFetchAPIãŸãã¯å¥ã®HTTPã¯ã©ã€ã¢ã³ããä»ããŠJSONããšã³ã³ãŒãããã³ãã³ãŒãããããã®çµã¿èŸŒã¿ã¡ãœããããããŸãããµãŒããŒåŽã®ãã¯ãããžãŒã¯ãã©ã€ãã©ãªã䜿çšããŠJSONããã³ãŒãããŸããããŠãŒã¶ãŒã®ä»å ¥ã¯ã»ãšãã©ãŸãã¯ãŸã£ãããããŸããã
ããŒã¿ã転éããæ¹æ³ã¯ä»ã«ããããŸãã XMLèªäœã¯ããã¬ãŒã ã¯ãŒã¯ã§ã¯ããŸãåºããµããŒããããŠããŸãããéåžžãããŒã¿ããã䟿å©ãªåœ¢åŒïŒéåžžã¯JSONïŒã«å€æããå¿ èŠããããŸããã¯ã©ã€ã¢ã³ãåŽãç¹ã«ãã©ãŠã¶ã§ã¯ããã®ããŒã¿ãåŠçããã®ã¯ããã»ã©ç°¡åã§ã¯ãããŸãããéåžžã®ããŒã¿è»¢éãä¿èšŒããããã ãã«ãå€ãã®è¿œå äœæ¥ãè¡ãå¿ èŠããããŸãã
ãã©ãŒã ã¯ãç¹ã«ãã¡ã€ã«ã転éããå Žåã«ãããŒã¿ã転éããã®ã«äŸ¿å©ã§ãããã ããããã¹ãããã³æ°å€åœ¢åŒã§æ å ±ã転éããå Žåãã»ãšãã©ã®ãã¬ãŒã ã¯ãŒã¯ã§ã¯è¿œå ã®åŠçãªãã§JSON転éãå¯èœã§ããããããã©ãŒã ãªãã§å®è¡ã§ããŸããã¯ã©ã€ã¢ã³ãåŽã§ããŒã¿ãååŸããã ãã§ããããã¯ããããã«å¯ŸåŠããããã®æãç°¡åãªæ¹æ³ã§ãã
ã¯ã©ã€ã¢ã³ããRESTAPIããåä¿¡ããJSONãJSONãšããŠæ£ç¢ºã«è§£éã§ããããã«ããã«ã¯ãèŠæ±ãè¡ãããåŸã
Content-Type
å¿çããããŒãå€application/json
ã«èšå®ããŸããå€ãã®ãµãŒããŒåŽã¢ããªã±ãŒã·ã§ã³ãã¬ãŒã ã¯ãŒã¯ã¯ãå¿çããããŒãèªåçã«èšå®ããŸããäžéšã®HTTPã¯ã©ã€ã¢ã³ãContent-Type
ã¯ãå¿çããããŒã調ã¹ãããã§æå®ããã圢åŒã«åŸã£ãŠããŒã¿ã解æããŸãã
å¯äžã®äŸå€ã¯ãã¯ã©ã€ã¢ã³ããšãµãŒããŒéã§è»¢éããããã¡ã€ã«ãéåä¿¡ããããšãããšãã«çºçããŸãã次ã«ãå¿çãšããŠåä¿¡ãããã¡ã€ã«ãåŠçãããã©ãŒã ããŒã¿ãã¯ã©ã€ã¢ã³ããããµãŒããŒã«éä¿¡ããå¿ èŠããããŸããããããããã¯å¥ã®èšäºã®ãããã¯ã§ãã
ãŸããJSONããšã³ããã€ã³ãããã®å¿çã§ããããšã確èªããå¿ èŠããããŸããå€ãã®ãµãŒããŒãã¬ãŒã ã¯ãŒã¯ã«ã¯ããã®æ©èœãçµã¿èŸŒãŸããŠããŸãã
JSONãã€ããŒããåãå ¥ããAPIã®äŸãèŠãŠã¿ãŸãããããã®äŸã§ã¯ãNode.jsçšã®Expressããã¯ãšã³ããã¬ãŒã ã¯ãŒã¯ã䜿çšããŠããŸããããã°ã©ã ãããã«ãŠã§ã¢ãšããŠäœ¿çšã
body-parser
ãŠJSONãªã¯ãšã¹ãã®æ¬æã解æres.json
ããJSONã¬ã¹ãã³ã¹ãšããŠè¿ããããªããžã§ã¯ãã䜿çšããŠã¡ãœãããåŒã³åºãããšãã§ããŸããããã¯æ¬¡ã®ããã«è¡ãããŸãã
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/', (req, res) => {
res.json(req.body);
});
app.listen(3000, () => console.log('server started'));
bodyParser.json()
ãªã¯ãšã¹ãæ¬æã®æååãJSONã«è§£æããJavaScriptãªããžã§ã¯ãã«å€æããŠãããçµæããªããžã§ã¯ãã«å²ãåœãŠãŸãreq.body
ã
å¿çã®Content-TypeããããŒã
application/json; charset=utf-8
å€æŽããã«å€ã«èšå®ããŸããäžèšã®æ¹æ³ã¯ãä»ã®ã»ãšãã©ã®ããã¯ãšã³ããã¬ãŒã ã¯ãŒã¯ã«é©çšã§ããŸãã
åè©ã§ã¯ãªãããšã³ããã€ã³ããžã®ãã¹ã«ååã䜿çšããŸã
ãšã³ããã€ã³ããžã®ãã¹ã®ååã¯ãåè©ã§ã¯ãªãååã«ããå¿ èŠããããŸãããã®ååã¯ãããããååŸããããŸãã¯æäœãããšã³ããã€ã³ãã®ãªããžã§ã¯ããè¡šããŸãã
éèŠãªã®ã¯ãHTTPãªã¯ãšã¹ãã¡ãœããã®ååã«ã¯ãã§ã«åè©ãå«ãŸããŠãããšããããšã§ãã APIãšã³ããã€ã³ããžã®ãã¹ã®ååã«åè©ãå ¥ããããšã¯å®çšçã§ã¯ãããŸãããããã«ãååãäžå¿ èŠã«é·ãã貎éãªæ å ±ãå«ãŸããŠããªãããšãå€æããŸãããéçºè ãéžãã åè©ã¯ã圌ã®æ°ãŸããã«å¿ããŠç°¡åã«çœ®ãããšãã§ããŸããããšãã°ããgetããªãã·ã§ã³ã奜ã人ãããã°ããretrieveãã奜ã人ãããã®ã§ããšã³ããã€ã³ããäœãããŠããã®ããæ£ç¢ºã«äŒããããªãã¿ã®HTTPGETåè©ã«éå®ããããšããå§ãããŸãã
ã¢ã¯ã·ã§ã³ã¯ãäœæãããªã¯ãšã¹ãã®HTTPã¡ãœããã®ååã§æå®ããå¿ èŠããããŸããæãäžè¬çãªã¡ãœããã«ã¯ãåè©GETãPOSTãPUTãããã³DELETEãå«ãŸããŠããŸãã
GETã¯ãªãœãŒã¹ããã§ããããŸããPOSTã¯æ°ããããŒã¿ããµãŒããŒã«éä¿¡ããŸããPUTã¯æ¢åã®ããŒã¿ãæŽæ°ããŸããDELETEã¯ããŒã¿ãåé€ããŸãããããã®ååè©ã¯ãCRUDã°ã«ãŒãã®æäœã®1ã€ã«å¯Ÿå¿ããŸãã
äžèšã®2ã€ã®ååãèæ ®ãããšãæ°ããèšäºãåãåãã«ã¯ãGET圢åŒã®ã«ãŒããäœæããå¿ èŠããããŸã
/articles/
ãåæ§ã«ãPOSTã䜿çš/articles/
ããŠæ°ããèšäº /articles/:id
ãæŽæ°ããPUTã䜿çšããŠç¹å®ã®èšäºã§èšäºãæŽæ°ããŸãid
ãDELETEã¡ãœãã㯠/articles/:id
ãæå®ãããIDãæã€èšäºãåé€ããããã«èšèšãããŠããŸãã
/articles
RESTAPIãªãœãŒã¹ã§ããããšãã°ãExpressã䜿çšããŠãèšäºã§æ¬¡ã®ããšãè¡ãããšãã§ããŸãã
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.get('/articles', (req, res) => {
const articles = [];
// ...
res.json(articles);
});
app.post('/articles', (req, res) => {
// ...
res.json(req.body);
});
app.put('/articles/:id', (req, res) => {
const { id } = req.params;
// ...
res.json(req.body);
});
app.delete('/articles/:id', (req, res) => {
const { id } = req.params;
// ...
res.json({ deleted: id });
});
app.listen(3000, () => console.log('server started'));
äžèšã®ã³ãŒãã§ã¯ãèšäºãæäœããããã®ãšã³ããã€ã³ããå®çŸ©ããŸãããã芧ã®ãšããããã¹åã«ã¯åè©ããããŸãããååã®ã¿ãåè©ã¯ãHTTPã¡ãœããã®ååã§ã®ã¿äœ¿çšãããŸãã
POSTãPUTãããã³DELETEãšã³ããã€ã³ãã¯ãJSONèŠæ±æ¬æãåãå ¥ããGETãšã³ããã€ã³ããå«ãJSONå¿çãè¿ããŸãã
ã³ã¬ã¯ã·ã§ã³ã¯è€æ°ã®åè©ãšåŒã°ããŸã
ã³ã¬ã¯ã·ã§ã³ã«ã¯ãè€æ°ã®åè©ãä»ããŠååãä»ããå¿ èŠããããŸããã³ã¬ã¯ã·ã§ã³ãã1ã€ã®ã¢ã€ãã ã ããååŸããå¿ èŠãããããšã¯ãã£ãã«ãªããããäžè²«æ§ãä¿ã¡ãã³ã¬ã¯ã·ã§ã³åã«è€æ°ã®åè©ã䜿çšããå¿ èŠããããŸãã
è€æ°ã¯ãããŒã¿ããŒã¹ã®åœåèŠåãšã®äžè²«æ§ãä¿ã€ããã«ã䜿çšãããŸããååãšããŠãããŒãã«ã«ã¯1ã€ã§ã¯ãªãå€ãã®ã¬ã³ãŒããå«ãŸããããã«å¿ããŠããŒãã«ã«ååãä»ããããŸãã
ãšã³ããã€ã³ã
/articles
ãæäœããå Žåããã¹ãŠã®ãšã³ããã€ã³ãã«ååãä»ãããšãã«è€æ°ã䜿çšããŸãã
éå±€ãªããžã§ã¯ããæäœãããšãã®ãªãœãŒã¹ã®ãã¹ã
ãã¹ãããããªãœãŒã¹ãåŠçãããšã³ããã€ã³ãã®ãã¹ã¯ã次ã®ããã«æ§æããå¿ èŠããããŸãããã¹ãããããªãœãŒã¹ãã芪ãªãœãŒã¹ã®ååã«ç¶ããã¹åãšããŠè¿œå ããŸãã
ã³ãŒãå ã®ãªãœãŒã¹ã®ãã¹ãããããŒã¿ããŒã¹ããŒãã«å ã®æ å ±ã®ãã¹ããšãŸã£ããåãã§ããããšã確èªããå¿ èŠããããŸããããããªããšãæ··ä¹±ãçããå¯èœæ§ããããŸãã
ããšãã°ãç¹å®ã®ãšã³ããã€ã³ãã§æ°ããèšäºã«é¢ããã³ã¡ã³ããåãåãããå Žåã¯ããã¹/ã³ã¡ã³ãããã¹ã®æåŸã«æ·»ä»ããå¿ èŠããããŸã
/articles
ããã®å Žåãcommentsãšã³ãã£ãã£ãarticle
ããŒã¿ããŒã¹å
ã®åãšã³ãã£ãã£ãšèŠãªããšæ³å®ãããŠããŸãã
ããšãã°ãExpressã§æ¬¡ã®ã³ãŒãã䜿çšããŠãããè¡ãããšãã§ããŸãã
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.get('/articles/:articleId/comments', (req, res) => {
const { articleId } = req.params;
const comments = [];
// articleId
res.json(comments);
});
app.listen(3000, () => console.log('server started'));
äžèšã®ã³ãŒãã§ã¯ããã¹ã§GETã¡ãœããã䜿çšã§ããŸã
'/articles/:articleId/comments'
ãcomments
äžèŽããèšäºãžã®ã³ã¡ã³ããåãåãarticleId
ãè¿ä¿¡ããŸãã'comments'
ãã¹ã»ã°ã¡ã³ãã®åŸã«è¿œå ããŠ'/articles/:articleId'
ããããåãªãœãŒã¹ã§ããããšã瀺ããŸã/articles
ã
ã³ã¡ã³ãã¯åãªããžã§ã¯ãã§
articles
ãããåèšäºã«ã¯ç¬èªã®ã³ã¡ã³ãã®ã»ããããããšæ³å®ãããŠãããããããã¯çã«ããªã£ãŠããŸããããããªããšããã®æ§é ã¯éåžžãåãªããžã§ã¯ãã«ã¢ã¯ã»ã¹ããããã«äœ¿çšãããããããŠãŒã¶ãŒãæ··ä¹±ãããå¯èœæ§ããããŸããPOSTãPUTãããã³DELETEãšã³ããã€ã³ããæäœããå Žåãåãååãé©çšãããŸãããããã¯ãã¹ãŠããã¹åãäœæãããšãã«ãã¹ãããåãæ§é ã䜿çšããŸãã
ãã¡ããšãããšã©ãŒåŠçãšæšæºãšã©ãŒã³ãŒããè¿ã
APIã§ãšã©ãŒãçºçãããšãã®æ··ä¹±ãé¿ããããã«ããšã©ãŒãæ éã«åŠçããçºçãããšã©ãŒã瀺ãHTTPå¿çã³ãŒããè¿ããŸããããã«ãããAPIã¡ã³ããã¯åé¡ãç解ããã®ã«ååãªæ å ±ãåŸãããšãã§ããŸãããšã©ãŒãã·ã¹ãã ãã¯ã©ãã·ã¥ãããããšã¯èš±å®¹ã§ããªãããããšã©ãŒãåŠçããã«æ®ãããšã¯ã§ãããAPIã³ã³ã·ã¥ãŒããŒã¯ãã®ãããªåŠçãåŠçããå¿ èŠããããŸãã
æãäžè¬çãªHTTPãšã©ãŒã³ãŒãã¯æ¬¡ã®ãšããã§ãã
- 400 BadRequest-ã¯ã©ã€ã¢ã³ãããåä¿¡ããå ¥åãæ€èšŒã«å€±æããããšã瀺ããŸãã
- 401 Unauthorized-ãŠãŒã¶ãŒããã°ã€ã³ããŠããªãããããªãœãŒã¹ã«ã¢ã¯ã»ã¹ããæš©éããªãããšãæå³ããŸããéåžžããã®ã³ãŒãã¯ããŠãŒã¶ãŒãèªèšŒãããŠããªããšãã«çºè¡ãããŸãã
- 403 Forbidden-ãŠãŒã¶ãŒã¯èªèšŒãããŠããŸããããªãœãŒã¹ã«ã¢ã¯ã»ã¹ããæš©éããªãããšã瀺ããŸãã
- 404 NotFound-ãªãœãŒã¹ãèŠã€ãããªãã£ãããšãæå³ããŸã
- 500å éšãµãŒããŒãšã©ãŒã¯ãµãŒããŒãšã©ãŒã§ãããæ瀺çã«ã¹ããŒããªãã§ãã ããã
- 502 BadGateway-ã¢ããã¹ããªãŒã ãµãŒããŒããã®ç¡å¹ãªå¿çã¡ãã»ãŒãžã瀺ããŸãã
- 503 Service Unavailable-ãµãŒããŒåŽã§äºæããªãããšãçºçããããšãæå³ããŸã-ããšãã°ããµãŒããŒã®éè² è·ãäžéšã®ã·ã¹ãã èŠçŽ ã®é害ãªã©ã
ã¢ããªã±ãŒã·ã§ã³ã劚ãããšã©ãŒã«å¯Ÿå¿ããã³ãŒããæ£ç¢ºã«çºè¡ããå¿ èŠããããŸããããšãã°ããªã¯ãšã¹ããã€ããŒããšããŠåä¿¡ããããŒã¿ãæåŠããå ŽåãExpress APIã®ã«ãŒã«ã«åŸã£ãŠã次ã®400ã®ã³ãŒããè¿ãå¿ èŠããããŸãã
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
//
const users = [
{ email: 'abc@foo.com' }
]
app.use(bodyParser.json());
app.post('/users', (req, res) => {
const { email } = req.body;
const userExists = users.find(u => u.email === email);
if (userExists) {
return res.status(400).json({ error: 'User already exists' })
}
res.json(req.body);
});
app.listen(3000, () => console.log('server started'));
äžèšã®ã³ãŒãã§ã¯ãusersé åã«ãé»åã¡ãŒã«ãç¥ã£ãŠããæ¢åã®ãŠãŒã¶ãŒã®ãªã¹ããä¿æããŠããŸãã
ããã«ã
email
ãŠãŒã¶ãŒã«ãã§ã«ååšããå€ã§ãã€ããŒããéä¿¡ããããšãããšãã³ãŒã400ã®å¿çãšã'User already exists'
ãã®ãããªãŠãŒã¶ãŒããã§ã«ååšããããšã瀺ãã¡ãã»ãŒãžã衚瀺ãããŸãããã®æ
å ±ã䜿çšããŠããŠãŒã¶ãŒã¯ããè¯ããªãããšãã§ããŸã-é»åã¡ãŒã«ã¢ãã¬ã¹ããŸã ãªã¹ãã«ãªããã®ã«çœ®ãæããŸãã
ãšã©ãŒã³ãŒãã«ã¯ããšã©ãŒãä¿®æ£ããã®ã«ååãªæ å ±ãæäŸããã¡ãã»ãŒãžãåžžã«æ·»ä»ããå¿ èŠããããŸãããæ å ±ãçãã ãã·ã¹ãã ãã¯ã©ãã·ã¥ããããããæ»æè ããã®æ å ±ã䜿çšããå¯èœæ§ãããã»ã©è©³çŽ°ã§ã¯ãããŸããã
APIãé©åã«ã·ã£ããããŠã³ã«å€±æããå Žåã¯åžžã«ããŠãŒã¶ãŒãç¶æ³ãä¿®æ£ããããããããã«ããšã©ãŒæ å ±ãéä¿¡ããŠå€±æãæ éã«åŠçããå¿ èŠããããŸãã
ããŒã¿ã®äžŠã¹æ¿ãããã£ã«ã¿ãªã³ã°ãããŒãžããŒã·ã§ã³ãèš±å¯ãã
RESTAPIã®èåŸã«ããåºç€ã¯å€§ããæé·ããå¯èœæ§ããããŸããããŒã¿ãå€ãããŠãäžåºŠã«ãã¹ãŠãåãæ»ãããšãã§ããªãå ŽåããããŸããããã«ãããã·ã¹ãã ã®é床ãäœäžããããã·ã¹ãã ãåæ¢ãããããå¯èœæ§ããããŸãããããã£ãŠãã¢ã€ãã ããã£ã«ã¿ãªã³ã°ããæ¹æ³ãå¿ èŠã§ãã
ãŸããäžåºŠã«å°æ°ã®çµæã®ã¿ãè¿ãããã«ãããŒã¿ãããŒãžåå²ããæ¹æ³ïŒããŒãžåå²ïŒãå¿ èŠã§ããèŠæ±ããããã¹ãŠã®ããŒã¿ãäžåºŠã«ååŸããããšãããªãœãŒã¹ã«æéãããããããªãããã«ããŸãã
ãã£ã«ã¿ãªã³ã°ãšããŒã¿ããŒãžããŒã·ã§ã³ã¯ã©ã¡ããããµãŒããŒãªãœãŒã¹ã®äœ¿çšãæžããããšã§ãããã©ãŒãã³ã¹ãåäžãããããšãã§ããŸããããŒã¿ããŒã¹ã«èç©ãããããŒã¿ãå€ãã»ã©ãããã2ã€ã®å¯èœæ§ãéèŠã«ãªããŸãã
ããã¯ãAPIãããŸããŸãªãã©ã¡ãŒã¿ãæã€ã¯ãšãªæååãåãå ¥ããããšãã§ããå°ããªäŸã§ãããã£ãŒã«ãã§ã¢ã€ãã ããã£ã«ã¿ãªã³ã°ããŠã¿ãŸãããã
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
//
const employees = [
{ firstName: 'Jane', lastName: 'Smith', age: 20 },
//...
{ firstName: 'John', lastName: 'Smith', age: 30 },
{ firstName: 'Mary', lastName: 'Green', age: 50 },
]
app.use(bodyParser.json());
app.get('/employees', (req, res) => {
const { firstName, lastName, age } = req.query;
let results = [...employees];
if (firstName) {
results = results.filter(r => r.firstName === firstName);
}
if (lastName) {
results = results.filter(r => r.lastName === lastName);
}
if (age) {
results = results.filter(r => +r.age === +age);
}
res.json(results);
});
app.listen(3000, () => console.log('server started'));
äžèšã®ã³ãŒãã«ã¯ã
req.query
ãªã¯ãšã¹ããã©ã¡ãŒã¿ãååŸã§ããå€æ°ããããŸãã次ã«ãåã
ã®ã¯ãšãªãã©ã¡ãŒã¿ãå€æ°ã«å解ããããšã§ãããããã£å€ãæœåºã§ããŸããJavaScriptã«ã¯ããã®ããã®ç¹å¥ãªæ§æããããŸãã
æåŸã«ãåã¯ãšãªãã©ã¡ãŒã¿å€ã«ãã£ã«ã¿ãé©çšããŠãè¿ãããã¢ã€ãã ãèŠã€ããŸãã
ãããå®äºãããšãå¿çãšããŠçµæãè¿ãããŸãããããã£ãŠãã¯ãšãªæååã䜿çšããŠæ¬¡ã®ãã¹ã«GETèŠæ±ãè¡ãå ŽåïŒ
/employees?lastName=Smith&age=30
æã ãåŸãïŒ
[
{
"firstName": "John",
"lastName": "Smith",
"age": 30
}
]
ãã£ã«ã¿ãªã³ã°ããªã³
lastName
ã§ãã£ãããã«è¿ãããå¿çãšããŠage
ã
åæ§ã«ãããŒãžã¯ãšãªãã©ã¡ãŒã¿ãåãå ¥ããŠããã
(page - 1) * 20
ãŸã§ã®äœçœ®ãå ããã¬ã³ãŒãã®ã°ã«ãŒããè¿ãããšãã§ããŸãpage * 20
ã
ãŸããã¯ãšãªæååã§ã䞊ã¹æ¿ããå®è¡ãããã£ãŒã«ããæå®ã§ããŸãããã®å Žåããããã®åå¥ã®ãã£ãŒã«ãã§äžŠã¹æ¿ããããšãã§ããŸããããšãã°ã次ã®ãããªURLããã¯ãšãªæååãæœåºããå¿ èŠãããå ŽåããããŸãã
http://example.com/articles?sort=+author,-datepublished
ãã
+
ã§ããäžããšâ
ãäžããæå³ããŸãããã®ãããèè
åãã¢ã«ãã¡ãããé ã«äžŠã¹æ¿ããå
¬éæ¥ãæ°ãããã®ããå€ããã®ãžãšäžŠã¹æ¿ããŸãã
å®çžŸã®ããã»ãã¥ãªãã£æ £è¡ãé å®ãã
æ©å¯æ å ±ãéåä¿¡ããããšãå€ããããã¯ã©ã€ã¢ã³ããšãµãŒããŒéã®éä¿¡ã¯ã»ãšãã©ãã©ã€ããŒãã«ããå¿ èŠããããŸãããããã£ãŠãã»ãã¥ãªãã£ã«SSL / TLSã䜿çšããå¿ èŠããããŸãã
SSL蚌ææžããµãŒããŒã«ã¢ããããŒãããã®ã¯ããã»ã©é£ãããªãã蚌ææžèªäœã¯ç¡æãŸãã¯éåžžã«å®äŸ¡ã§ãã REST APIããªãŒãã³ãã£ãã«ã§ã¯ãªããå®å šãªãã£ãã«ãä»ããŠéä¿¡ã§ããããã«ããããšãè«Šããçç±ã¯ãããŸããã
人ã¯åœŒãèŠæ±ããããå€ãã®æ å ±ãžã®ã¢ã¯ã»ã¹ãäžããããã¹ãã§ã¯ãããŸãããããšãã°ãéåžžã®ãŠãŒã¶ãŒã¯å¥ã®ãŠãŒã¶ãŒã®æ å ±ã«ã¢ã¯ã»ã¹ããã¹ãã§ã¯ãããŸããããŸãã管çè ã®ããŒã¿ã衚瀺ã§ããªãããã«ããå¿ èŠããããŸãã
æå°ç¹æš©ã®ååãæšé²ããã«ã¯ãç¹å®ã®åœ¹å²ã®åœ¹å²ãã§ãã¯ãå®è£ ããããåãŠãŒã¶ãŒã«å¯ŸããŠãã詳现ãªåœ¹å²ãæäŸããå¿ èŠããããŸãã
ãŠãŒã¶ãŒãè€æ°ã®ããŒã«ã«ã°ã«ãŒãåããå Žåã¯ããŠãŒã¶ãŒãå¿ èŠãšãããã¹ãŠã®ããšãå®è¡ãããã以äžå®è¡ããªãããã«ããã¢ã¯ã»ã¹æš©ãããŒã«ã«ä»äžããå¿ èŠããããŸãããŠãŒã¶ãŒã«æäŸãããåæ©äŒãžã®ã¢ã¯ã»ã¹æš©ããã詳现ã«èŠå®ããå Žåã管çè ããããã®æ©èœããã¹ãŠã®ãŠãŒã¶ãŒã«ä»äžã§ããããã«ãããããããã®æ©èœãåé€ã§ããããã«ããå¿ èŠããããŸããããã«ããŠãŒã¶ãŒã°ã«ãŒãã«é©çšã§ããäºåå®çŸ©ããã圹å²ãããã€ãè¿œå ããŠãåãŠãŒã¶ãŒã«å¿ èŠãªæš©éãæåã§èšå®ããå¿ èŠããªãããã«ããå¿ èŠããããŸãã
ããŒã¿ããã£ãã·ã¥ããŠããã©ãŒãã³ã¹ãåäžããã
ãã£ãã·ã³ã°ãè¿œå ããŠããŠãŒã¶ãŒãèŠæ±ãããã³ã«ããŒã¿ããŒã¹ããããŒã¿ãååŸããã®ã§ã¯ãªããããŒã«ã«ã¡ã¢ãªãã£ãã·ã¥ããããŒã¿ãè¿ãããšãã§ããŸãããã£ãã·ã³ã°ã®å©ç¹ã¯ããŠãŒã¶ãŒãããŒã¿ãããéãååŸã§ããããšã§ãããã ãããã®ããŒã¿ã¯å€ããªã£ãŠããå¯èœæ§ããããŸããããã¯ãå®çšŒåç°å¢ã§ãããã°ãããšããåé¡ãçºçããŠå€ãããŒã¿ã調ã¹ç¶ãããšãã«ãåé¡ãçºçããå¯èœæ§ããããŸããRedisãã¡ã¢ãªå ãã£ãã·ã³ã°
ãªã©ãããŸããŸãªãã£ãã·ã³ã°ãªãã·ã§ã³ãå©çšã§ããŸããå¿ èŠã«å¿ããŠãããŒã¿ã®ãã£ãã·ã¥æ¹æ³ãå€æŽã§ããŸãã ããšãã°ãExpressã¯ããã«ãŠã§ã¢ãæäŸããŸã
apicache
è€éãªæ§æãè¡ããã«ãã¢ããªã±ãŒã·ã§ã³ã«ãã£ãã·ã¥æ©èœãè¿œå ããŸããåçŽãªã¡ã¢ãªå
ãã£ãã·ã¥ã¯ã次ã®ããã«ãµãŒããŒã«è¿œå ã§ããŸãã
const express = require('express');
const bodyParser = require('body-parser');
const apicache = require('apicache');
const app = express();
let cache = apicache.middleware;
app.use(cache('5 minutes'));
//
const employees = [
{ firstName: 'Jane', lastName: 'Smith', age: 20 },
//...
{ firstName: 'John', lastName: 'Smith', age: 30 },
{ firstName: 'Mary', lastName: 'Green', age: 50 },
]
app.use(bodyParser.json());
app.get('/employees', (req, res) => {
res.json(employees);
});
app.listen(3000, () => console.log('server started'));
äžèšã®ã³ãŒãã¯åã«
apicache
withãåç
§ããŠããããapicache.middleware
ã次ã®ããã«ãªããŸãã
app.use(cache('5 minutes'))
ã¢ããªã±ãŒã·ã§ã³å šäœã®ãã£ãã·ã³ã°ãé©çšããã«ã¯ãããã§ååã§ããããšãã°ããã¹ãŠã®çµæã5åã§ãã£ãã·ã¥ããŸãããã®åŸããã®å€ã¯å¿ èŠã«å¿ããŠèª¿æŽã§ããŸãã
APIããŒãžã§ã³ç®¡ç
ã¯ã©ã€ã¢ã³ããæ··ä¹±ãããå¯èœæ§ã®ããå€æŽãå ããå Žåã«åããŠãAPIã®ããŒãžã§ã³ãå€ããå¿ èŠããããŸããããŒãžã§ã³ç®¡çã¯ã»ãã³ãã£ãã¯ããŒã¹ã§å®è¡ã§ããŸãïŒããšãã°ã2.0.6ã¯ã¡ãžã£ãŒããŒãžã§ã³ã2ã§ããããšãæå³ããããã¯6çªç®ã®ãããã§ãïŒããã®ååã¯çŸåšãã»ãšãã©ã®ã¢ããªã±ãŒã·ã§ã³ã§åãå ¥ããããŠããŸãã
ãã®ããã«ããŠãå šå¡ãåæã«æ°ããAPIã«åãæ¿ããå¿ èŠããªããå€ããšã³ããã€ã³ããåŸã ã«å»æ¢ã§ããŸããäœãå€æŽããããªã人ã®ããã«v1ããŒãžã§ã³ãä¿åããã¢ããã°ã¬ãŒãã®æºåãã§ããŠãã人ã®ããã«v2ããŒãžã§ã³ã«ãã¹ãŠã®æ°æ©èœãæäŸããããšãã§ããŸããããã¯ããããªãã¯APIã®ã³ã³ããã¹ãã§ç¹ã«éèŠã§ãã APIã䜿çšãããµãŒãããŒãã£ã®ã¢ããªã±ãŒã·ã§ã³ãç Žæããªãããã«ãããŒãžã§ã³ç®¡çããå¿ èŠããããŸãã
ããŒãžã§ã³ç®¡çã¯éåžžããè¿œå ããŠè¡ãããŸã
/v1/
ã/v2/
ããªã©ãAPIãã¹ã®å
é ã«è¿œå ãããŸãã
ããšãã°ãExpressã§ãããè¡ãæ¹æ³ã¯æ¬¡ã®ãšããã§ãã
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.get('/v1/employees', (req, res) => {
const employees = [];
//
res.json(employees);
});
app.get('/v2/employees', (req, res) => {
const employees = [];
//
res.json(employees);
});
app.listen(3000, () => console.log('server started'));
ãšã³ããã€ã³ãã«ã€ãªãããã¹ã®å é ã«ããŒãžã§ã³çªå·ãè¿œå ããã ãã§ãã
çµè«
é«å質ã®RESTAPIãèšèšããéã®éèŠãªãã€ã³ãã¯ãWebã®æšæºãšèŠåã«åŸãããšã§äžè²«æ§ãç¶æããããšã§ããJSONãSSL / TLSãããã³HTTPã¹ããŒã¿ã¹ã³ãŒãã¯ãä»æ¥ã®Webã«ãªããŠã¯ãªããªããã®ã§ãã
ããã©ãŒãã³ã¹ãåæ§ã«éèŠã§ããäžåºŠã«å€ãã®ããŒã¿ãè¿ããã«å¢ããããšãã§ããŸããããã«ããã£ãã·ã³ã°ã䜿çšããŠãåãããŒã¿ãäœåºŠãèŠæ±ããããšãåé¿ã§ããŸãã
ãšã³ããã€ã³ããã¹ã«ã¯äžè²«ããååãä»ããå¿ èŠããããŸããHTTPã¡ãœããã®ååã«ã¯åè©ãå«ãŸããŠãããããååã«ã¯åè©ã䜿çšããå¿ èŠããããŸãããã¹ãããããªãœãŒã¹ãã¹ã¯ã芪ãªãœãŒã¹ãã¹ã«åŸãå¿ èŠããããŸããäœãèµ·ãã£ãŠããã®ããç解ããããã«ããã¥ã¡ã³ããããã«åç §ããå¿ èŠããªãããã«ã圌ãã¯ç§ãã¡ãåãåã£ããæäœããããããã®ãäŒããå¿ èŠããããŸãã