{"id":210,"date":"2015-10-20T11:41:29","date_gmt":"2015-10-20T02:41:29","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/10\/20\/favorite-node-modules"},"modified":"2015-10-20T11:41:29","modified_gmt":"2015-10-20T02:41:29","slug":"favorite-node-modules","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2015\/10\/20\/favorite-node-modules\/","title":{"rendered":"\u30aa\u30b9\u30b9\u30e1node-modules"},"content":{"rendered":"
\u624b\u6307\u304c\u30ab\u30b5\u3064\u304d\u904e\u304e\u3066\u6307\u7d0b\u8a8d\u8a3c\u304c\u51fa\u6765\u306a\u3044\u5c0f\u98fc\u3067\u3059\u3002<\/p>\n
\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u958b\u767a\u306b\u304a\u3044\u3066\u30c7\u30d5\u30a1\u30af\u30c8\u30b9\u30bf\u30f3\u30c0\u30fc\u30c9\u3068\u306a\u3063\u305f\u611f\u306e\u3042\u308b validator<\/a><\/p>\n \u4e0e\u3048\u3089\u308c\u305f\u6587\u5b57\u5217\u3092\u691c\u8a3c\u3057\u305f\u308a\u7121\u5bb3\u5316\u3057\u307e\u3059\u3002<\/p>\n escape-regexp<\/a><\/p>\n \u6b63\u898f\u8868\u73fe\u7528\u306b\u6587\u5b57\u5217\u3092\u30a8\u30b9\u30b1\u30fc\u30d7\u3057\u307e\u3059\u3002<\/p>\n uuid<\/a><\/p>\n \u30e6\u30cb\u30fc\u30af\u306a\u6587\u5b57\u5217\u3092\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n autolinker<\/a><\/p>\n \u4e0e\u3048\u305f\u6587\u5b57\u5217\u5185\u306eURL, \u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9, Twitter\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u5224\u5225\u3057\u3066\u30ea\u30f3\u30af\u5316\u3057\u307e\u3059\u3002<\/p>\n bcrypt<\/a><\/p>\n \u4e3b\u306b\u30d1\u30b9\u30ef\u30fc\u30c9\u306a\u3069\u306e\u6697\u53f7\u5316\u306b\u7528\u3044\u308bbcrypt\u6697\u53f7\u5316\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3002 \u53c2\u8003: \u7b46\u8005\u306e\u958b\u767a\u74b0\u5883\u3067\u6697\u53f7\u5316\u90e8\u5206\u306e\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092npm<\/code>\u306f\u3001
\nNode.js<\/code>\u30b3\u30df\u30e5\u30cb\u30c6\u30a3\u306b\u3088\u308b\u958b\u767a\u306e\u6d3b\u767a\u306a\u7121\u6570\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u7fa4\u304c\u3042\u308a\u3001
Node.js<\/code>\u305d\u306e\u3082\u306e\u306e\u9b45\u529b\u306b\u5927\u304d\u304f\u8ca2\u732e\u3057\u3066\u3044\u307e\u3059\u3002
\n\u305d\u3053\u3067\u672c\u7a3f\u3067\u306f\u79c1\u306e\u6c17\u306b\u5165\u3063\u3066\u3044\u308bnpm<\/code>\u3092\u3001\u6d3b\u7528\u7bc4\u56f2\u3092\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306b\u9650\u3089\u305a\u3044\u304f\u3064\u304b\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n
validator<\/h3>\n
import validator from 'validator';\nconst inValidMail = 'test@test,com'\nconst dangerInput = '<script>alert("\u5b9f\u884c\u53ef\u80fd")<\/script>'\nconsole.log(validator.isEmail(inValidMail)); \/\/ false\nconsole.log(validator.escape(dangerInput)); \/\/ <script>alert("\u5b9f\u884c\u53ef\u80fd")</script><\/code><\/pre>\n
escape-regexp<\/h3>\n
import escape from 'escape-regexp';\nconst dangerStr = '\u305d\u306e\u307e\u307e\u3067\u306f\/\u4f7f\u3048\u306a\u3044\/';\nconsole.log(escape(dangerStr)); \/\/ \u305d\u306e\u307e\u307e\u3067\u306f\/\u4f7f\u3048\u306a\u3044\/<\/code><\/pre>\n
uuid<\/h3>\n
import uuid from 'uuid';\nconst mytoken = uuid.v1();\nconsole.log(mytoken); \/\/ 19943a60-7182-11e5-9c4f-3738ae6ba773<\/code><\/pre>\n
autolinker<\/h3>\n
import Autolinker from 'autolinker';\nconst linkedText = Autolinker.link('\u30d1\u30c3\u30b1\u30fc\u30b8\u306eURL\u306fhttps:\/\/www.npmjs.com\/package\/autolinker\u3067\u3059');\nconsole.log(linkedText);\n\/\/ \u30d1\u30c3\u30b1\u30fc\u30b8\u306eURL\u306f<a href="https:\/\/www.npmjs.com\/package\/autolinker" target="_blank">npmjs.com\/package\/autolinker<\/a>\u3067\u3059<\/code><\/pre>\n
bcrypt<\/h3>\n
\n\u5b89\u5168\u6027\u3068\u6bd4\u4f8b\u3057\u3066\u51e6\u7406\u6642\u9593\u306f\u9577\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\nimport bcrypt from 'bcryptjs'\nimport Promise from 'bluebird'\n\n\/\/ \u6697\u53f7\u5316\u6f14\u7b97\u306e\u56de\u6570 2\u306e10\u4e57\n\/\/ \u3053\u306e\u6570\u5b57\u306b\u6bd4\u4f8b\u3057\u3066\nconst SALT_WORK_FACTOR = 10;\n\n\/\/ \u540c\u671f\u51e6\u7406\u7528\u306eAPI\u3082\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u304c\u3001\n\/\/ \u6697\u53f7\u5316\u51e6\u7406\u306e\u91cd\u3055\u3092\u8003\u3048\u975e\u540c\u671f\u51e6\u7406\u3092\u63a1\u7528\u3057\u305f\nfunction generateSalt(rawPassword) {\n return new Promise((resolve, reject)=> {\n bcrypt.genSalt(SALT_WORK_FACTOR, (err, salt)=> {\n if (err) {\n return reject(err);\n }\n resolve({ rawPassword, salt });\n });\n });\n}\n\nfunction hashPassword({ rawPassword, salt }) {\n return new Promise((resolve, reject)=> {\n bcrypt.hash(rawPassword, salt, (err, hash)=> {\n if (err) {\n return reject(err);\n }\n resolve(hash);\n });\n });\n}\n\nfunction saveDB(hashedPassword) {\n return new Promise((resolve, reject)=> {\n \/\/ DB\u4fdd\u5b58\u306e\u51e6\u7406\n });\n}\n\ngenerateSalt()\n.then(hashPassword)\n.then(saveDB)\n.finally(next);\n\n\/\/ \u5fa9\u5143\u3057\u3066\u691c\u8a3c\nfunction inspectionPassword(candidatePassword, hashedPassword) {\n return new Promise((resolve, reject)=> {\n bcrypt.compare(candidatePassword, hashedPassword, (err, isMatch)=> {\n if (err) {\n return reject(err);\n }\n resolve(isMatch);\n });\n });\n}<\/code><\/pre>\n
SALT_WORK_FACTOR<\/code>\u5b9a\u6570\u306e\u5024\u3092\u5909\u3048\u3066\u8d70\u3089\u305b\u3066\u307f\u305f\u7d50\u679c<\/p>\n