{"id":452,"date":"2018-05-09T10:45:37","date_gmt":"2018-05-09T01:45:37","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/05\/09\/node-express"},"modified":"2018-05-09T10:45:37","modified_gmt":"2018-05-09T01:45:37","slug":"node-express","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/05\/09\/node-express\/","title":{"rendered":"Node + Express\u3067API\u958b\u767a\u3092\u3059\u308b\u6642\u306e\u958b\u767a\u74b0\u5883"},"content":{"rendered":"
Node.js + Express\u3067API\u958b\u767a\u3092\u3059\u308b\u6642\u306e\u74b0\u5883\u306b\u3064\u3044\u3066\u66f8\u304d\u307e\u3059\u3002<\/p>\n
(\u306a\u305cNode\u3092\u9078\u5b9a\u3057\u305f\u304b\u306b\u3064\u3044\u3066\u306f\u304a\u5ba2\u69d8\u306e\u8981\u671b\u3082\u3042\u308a\u304d\u306e\u304a\u8a71\u3060\u3063\u305f\u306e\u3067\u3001\u3053\u3053\u3067\u306f\u8a71\u984c\u3068\u3057\u307e\u305b\u3093)\u3002<\/p>\n
\u4f7f\u7528\u3057\u3066\u3044\u308b\u6280\u8853\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n
\u4ee5\u4e0b\u3001\u9806\u306b\u307f\u3066\u3086\u304d\u307e\u3059\u3002<\/p>\n
\u958b\u767a\u74b0\u5883\u306fDocker\u3067\u30b3\u30f3\u30c6\u30ca\u5316\u3057\u3066\u3044\u307e\u3059\u3002\u30c7\u30d0\u30c3\u30b0\u3084\u30c4\u30fc\u30eb\u306e\u5c0e\u5165\u306a\u3069\u67d4\u8edf\u306b\u884c\u3048\u308b\u3088\u3046\u3001ubuntu\u74b0\u5883\u306bNode\u3092\u3044\u308c\u3066\u3044\u307e\u3059\u3002\u52a0\u3048\u3066\u3001MySQL\u3068nginx\u306e\u30b3\u30f3\u30c6\u30ca\u3092\u30ea\u30f3\u30af\u3057\u3066\u3044\u308b\u611f\u3058\u3067\u3059\u3002<\/p>\n
\u3088\u304f\u3042\u308bNode\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3063\u307d\u304f\u3001yarn\u30b3\u30de\u30f3\u30c9\u3067\u5168\u822c\u7684\u306b\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u3066\u3001\u4f8b\u3048\u3070\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30b3\u30f3\u30c6\u30ca\u8d77\u52d5\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
\/* package.json *\/\n{\n "docker:dev": "docker-compose -f docker-compose.yml -f docker-compose.dev.yml up",\n "docker:prod": "docker-compose -f docker-compose.yml -f docker-compose.prod.yml up" \n}<\/code><\/pre>\n\u666e\u6bb5JS\u3092\u4f7f\u308f\u306a\u3044\u4eba\u306f\u3001yarn\u3092\u30ed\u30fc\u30ab\u30eb\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3001\u4e0d\u8a55\u3082\u3042\u308a\u307e\u3057\u305f\u6c57\u3002\u3053\u3053\u306f\u3044\u3044\u611f\u3058\u306b\u30b3\u30f3\u30c6\u30ca\u306b\u9589\u3058\u3089\u308c\u308c\u3070\u305d\u306e\u307b\u3046\u304c\u3044\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n
Babel \/ Flow<\/h2>\n
\u30d3\u30eb\u30c9\u3068\u3044\u3046\u30d3\u30eb\u30c9\u3082\u3057\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u3068\u3057\u3066Babel\u3060\u3051\u3044\u308c\u3066\u3044\u307e\u3059\u3002Node\u306f\u6700\u65b0\u306eECMAScript\u3067\u3042\u3063\u3066\u3082\u4e00\u901a\u308a\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u304c\u3001Flow\u7b49\u306e\u30c4\u30fc\u30eb\u3084\u3001\u30e9\u30a4\u30d6\u30e9\u30ea\u9593\u306e\u4f9d\u5b58\u3092\u306a\u304f\u3059\u305f\u3081\u306b\u3044\u308c\u3066\u304a\u304d\u307e\u3057\u305f\u3002<\/p>\n
\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f\u3001async\/await\u3092\u4f7f\u3063\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u306e\u304c\u3044\u3044\u304b\u306a\u3068\u601d\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
import type { $Request, $Response } from 'express';\nimport {db} from '..\/models';\nimport {response} from '..\/services';\n\nconst getResource = async (req: $Request, res: $Response) => {\n try {\n if (response.handleParameterError(req, res)) {\n return;\n }\n\n const resource = await db.Resource.findById(req.params.id);\n\n if (!resource) {\n response.notFound(res, '\u30ea\u30bd\u30fc\u30b9\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002');\n return;\n }\n\n response.ok(res, {\n id: resource.id,\n name: resource.name,\n status: resource.status,\n });\n } catch (e) {\n response.internalServerError(res, e);\n }\n};<\/code><\/pre>\n\u9759\u7684\u578b\u4ed8\u3051\u306fFlow\u3092\u4f7f\u3063\u3066\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
ORM<\/h2>\n
ORM\u3068\u3057\u3066Sequelize<\/a>\u3092\u3044\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\nmigration\u3084\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306a\u3069\u3001\u6700\u4f4e\u9650\u306e\u3053\u3068\u306f\u4e00\u901a\u308a\u3067\u304d\u307e\u3059\u304c\u3001\u3088\u304f\u306a\u3044\u306a\u3042\u3068\u601d\u3046\u3053\u3068\u3082\u3042\u3063\u305f\u306e\u3067\u3001\u305d\u308c\u306f\u307e\u305f\u5225\u306e\u6a5f\u4f1a\u3067\u53d6\u308a\u4e0a\u3052\u3066\u307f\u3088\u3046\u304b\u3068\u601d\u3044\u307e\u3059...\u3002(TypeORM<\/a>\u3084Waterline<\/a>\u306a\u3069\u3092\u4f7f\u3063\u3066\u307f\u3066\u3082\u826f\u3044\u304b\u3082?)\u3002<\/p>\n\u30c6\u30b9\u30c8<\/h2>\n
\u30c6\u30b9\u30c8\u65b9\u91dd\u3068\u3057\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5177\u5408\u3067\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306e\u30c6\u30b9\u30c8\u3092\u3001\u7d50\u5408\u30c6\u30b9\u30c8\u7684\u306b\u3001\u30ea\u30af\u30a8\u30b9\u30c8\u3068\u305d\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u30c6\u30b9\u30c8\u3057\u3066\u3044\u307e\u3059\u3002
\n\u305d\u306e\u4ed6\u3001\u91cd\u8981\u306a\u30ed\u30b8\u30c3\u30af\u306e\u306f\u3044\u3063\u305f\u90e8\u5206\u3067\u306f\u3001\u5225\u9014\u95a2\u6570\u306e\u30c6\u30b9\u30c8\u3082\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
import request from 'supertest';\nimport assert from 'assert';\nimport {app} from '..\/app';\n\ndescribe('\/resources\/:id', () => {\n beforeEach(async () => {\n await cleanDatabase();\n await mock.resources.single(); \/\/ DB\u306b\u30e2\u30c3\u30af\u3092\u751f\u6210\n });\n it('Resource\u8a73\u7d30\u60c5\u5831\u304c\u53d6\u5f97\u3067\u304d\u308b', async () => {\n const req = request(app).get('\/resources\/1');\n await req\n .expect(200)\n .then((res) => {\n assert.equal(res.body.id, 1);\n assert.equal(res.body.name, 'NAME_0');\n assert.equal(res.body.status, 'valid');\n });\n });\n});<\/code><\/pre>\n\u307e\u305f\u3001eslint\u3092\u4f7f\u3063\u3066\u30ea\u30f3\u30c8\u3092\u884c\u3063\u3066\u304a\u308a\u3001\u578b\u30c1\u30a7\u30c3\u30af\u3082\u5408\u308f\u305b\u3066\u3001Circle CI\u3067\u5168\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
\u5165\u308c\u3066\u304a\u304f\u3068\u826f\u3055\u305d\u3046\u306a\u30e9\u30a4\u30d6\u30e9\u30ea<\/h2>\n
Node\u306e\u54f2\u5b66<\/a>\u3068\u3057\u3066\u300c\u4e00\u3064\u306e\u3053\u3068\u3092\u3046\u307e\u304f\u3084\u308b\u306e\u304c\u826f\u3044\u300d\u3068\u3044\u308f\u308c\u307e\u3059\u304c\u3001\u826f\u304f\u3082\u60aa\u304f\u3082\u3001Express\u306f\u305d\u308c\u3092\u3088\u304f\u8868\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\u3072\u3068\u3064\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u4e00\u3064\u306e\u3053\u3068\u3060\u3051\u3092\u884c\u3044\u3001\u4f55\u304b\u3092\u5b9f\u73fe\u3057\u305f\u3044\u6642\u306f\u5225\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3001\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u3044\u304f\u3064\u304b\u6700\u521d\u304b\u3089\u5165\u308c\u3066\u304a\u304f\u3068\u3044\u3044\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u30d4\u30c3\u30af\u30a2\u30c3\u30d7\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n
\n- body-parser<\/a> - JSON\u306a\u3069\u306erequest body\u3092\u3044\u3044\u611f\u3058\u306b\u5909\u63db<\/li>\n
- express-validator<\/a> - validator\u3092\u30d9\u30fc\u30b9\u3068\u3057\u305f\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3<\/li>\n
- morgan<\/a> - HTTP request logger<\/li>\n
- bluebird<\/a> - Promise\u30e9\u30c3\u30d1\u30fc<\/li>\n
- lodash<\/a> - \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3084\u914d\u5217\u64cd\u4f5c<\/li>\n
- moment<\/a> - \u6642\u9593\u64cd\u4f5c<\/li>\n
- dotenv<\/a> - \u74b0\u5883\u5909\u6570<\/li>\n<\/ul>\n
\n\u4ee5\u4e0a\u3001\u7c21\u5358\u306b\u3067\u3059\u304c\u3001Node\u3067API\u958b\u767a\u3092\u3059\u308b\u6642\u306e\u74b0\u5883\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002\u53c2\u8003\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
Node.js + Express\u3067API\u958b\u767a\u3092\u3059\u308b\u6642\u306e\u74b0\u5883\u306b\u3064\u3044\u3066\u66f8\u304d\u307e\u3059\u3002 (\u306a\u305cNode\u3092\u9078\u5b9a\u3057\u305f\u304b\u306b\u3064\u3044\u3066\u306f\u304a\u5ba2\u69d8\u306e\u8981\u671b\u3082\u3042\u308a\u304d\u306e\u304a\u8a71\u3060\u3063\u305f\u306e\u3067\u3001\u3053\u3053\u3067\u306f\u8a71\u984c\u3068\u3057\u307e\u305b\u3093)\u3002 \u6982\u8981 \u4f7f\u7528\u3057\u3066\u3044\u308b\u6280\u8853\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002 Node Express Docker Yarn Babel Flow MySQL Sequelize \u30c6\u30b9\u30c8 Mocha supertest ESLint Circle CI […]<\/p>\n","protected":false},"author":1,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[264],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/452"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=452"}],"version-history":[{"count":0,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/452\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}