{"id":6536,"date":"2011-11-27T04:29:31","date_gmt":"2011-11-26T19:29:31","guid":{"rendered":"http:\/\/fukata.org\/?p=6536"},"modified":"2017-08-11T22:58:48","modified_gmt":"2017-08-11T22:58:48","slug":"nodejs-node-config","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/6536\/","title":{"rendered":"node.js\u3067\u74b0\u5883\u5225\u8a2d\u5b9a\u3092\u884c\u3046\u300cnode-config\u300d"},"content":{"rendered":"

lorenwest\/node-config – GitHub<\/a><\/p>\n

node.js\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3082\u74b0\u5883\u306e\u5207\u308a\u5206\u3051\u8a2d\u5b9a\u304c\u5168\u7136\u51fa\u6765\u3066\u3044\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u4e0a\u8a18\u306enode-config\u3068\u3044\u3046\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u3063\u3066\u307f\u308b\u4e8b\u306b\u3057\u307e\u3059\u3002<\/p>\n

\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306f\u3001js, json, yaml\u3067\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n

\u3067\u306f\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304b\u3089\u3002\u30e2\u30b8\u30e5\u30fc\u30eb\u540d\u3068\u9055\u3046\u306e\u3067\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

\r\n$ npm install config\r\n<\/pre>\n

\u5b9f\u969b\u306b\u74b0\u5883\u5225\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u683c\u7d0d\u3059\u308b\u5834\u6240\u3067\u3059\u304c\u3001node\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3042\u308b\u300cconfig\u300d\u3068\u3044\u3046\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4e2d\u306b\u683c\u7d0d\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n

\u4f8b\u3068\u3057\u3066\u3001express\u3092\u4f7f\u3063\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u6210\u3060\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

\r\n.\r\n\u251c\u2500\u2500 app.js\r\n\u251c\u2500\u2500 config # \u74b0\u5883\u5225\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u7528\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\r\n\u2502   \u251c\u2500\u2500 default.js # \u30c7\u30d5\u30a9\u30eb\u30c8\u7528\r\n\u2502   \u251c\u2500\u2500 development.js # \u958b\u767a\u74b0\u5883\u7528\r\n\u2502   \u251c\u2500\u2500 test.js # \u30c6\u30b9\u30c8\u74b0\u5883\u7528\r\n\u2502   \u2514\u2500\u2500 production.js # \u672c\u756a\u74b0\u5883\u7528\r\n\u251c\u2500\u2500 node_modules\r\n\u251c\u2500\u2500 package.json\r\n\u251c\u2500\u2500 public\r\n\u251c\u2500\u2500 routes\r\n\u2514\u2500\u2500 views\r\n<\/pre>\n

\u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u62e1\u5f35\u5b50\u304c\u300cjs\u300d\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u304c\u3001json\u307e\u305f\u306fyml\u3067\u3082\u5927\u4e08\u592b\u3067\u3059\u3002<\/p>\n

\u3061\u306a\u307f\u306b\u3001\u5404\u74b0\u5883\u5225\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u8eab\u306fjs\u306e\u5834\u5408\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

\r\nmodule.exports = {\r\n  \"DB\": {\r\n    \"name\": \"example_dev\",\r\n    \"host\": \"localhost\"\r\n  }\r\n};\r\n<\/pre>\n

json, yaml\u306e\u5834\u5408\u306f\u3001\u5358\u306b\u305d\u308c\u305e\u308c\u306e\u30c7\u30fc\u30bf\u5f62\u5f0f\u3067\u66f8\u3051\u3070\u826f\u3044\u3060\u3051\u3067\u3059\u3002<\/p>\n

\u5b9f\u969b\u306b\u4f7f\u3046\u5074\u3067\u306f\u3001\u3053\u3093\u306a\u611f\u3058\u3067\u4f7f\u3048\u307e\u3059\u3002<\/p>\n

\r\nvar config = require('config');\r\nconsole.log(config.DB.name);\r\nconsole.log(config.DB.host);\r\n<\/pre>\n

node-config\u306f\u74b0\u5883\u5909\u6570\u300cNODE_ENV\u300d\u306e\u5024\u3092\u898b\u3066\u8aad\u307f\u8fbc\u3080\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u6c7a\u3081\u3066\u3044\u307e\u3059\u3002\u30ef\u30f3\u30e9\u30a4\u30ca\u30fc\u3067\u74b0\u5883\u306e\u6307\u5b9a\u3068\u5b9f\u884c\u3092\u3059\u308b\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

\r\n$ NODE_ENV=development node app.js\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

lorenwest\/node-config – GitHub node.js\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u3082\u74b0\u5883\u306e\u5207\u308a\u5206\u3051\u8a2d\u5b9a\u304c\u5168\u7136\u51fa\u6765\u3066\u3044\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u4e0a\u8a18\u306enode-config\u3068\u3044\u3046\u30e2\u30b8\u30e5\u30fc\u30eb … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[943],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/6536"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=6536"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/6536\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=6536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=6536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=6536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}