{"id":2964,"date":"2016-01-09T02:21:13","date_gmt":"2016-01-09T02:21:13","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2964"},"modified":"2016-01-09T02:21:13","modified_gmt":"2016-01-09T02:21:13","slug":"node-commonjs-example-library-template","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/node-commonjs-example-library-template\/","title":{"rendered":"Node CommonJS Example Library Template"},"content":{"rendered":"<p>If you want to make a new CommonJS module for Node, you can use the following as a template file (we&#8217;ll call this &#8216;detector.js&#8217;, but obviously you can name it whatever you want):<\/p>\n<pre lang=\"javascript\">\n\"use strict\";\n\nlet _ = require('lodash');\n\nexports.functionA =\n  (data) => {\n    return data;\n  };\n\nexports.functionB =\n  (data) => {\n    return data;\n  };\n<\/pre>\n<p>Then, when you want to use it, you can pull it in with require (note this is in the same folder as the module I&#8217;ve named detector):<\/p>\n<pre lang=\"javascript\">\n\"use strict\";\n\nlet express = require('express'),\n    _ = require('lodash'),\n    Sequelize = require('sequelize'),\n    passport = require('passport'),\n    http = require('http'),\n    db = require('.\/models'),\n    detector = require('.\/detector');\n\napp.get('\/preview\/:alert_id', (req, res) => {\n  let id = req.params.alert_id;\n\n  db.Alert.find(\n    {where: {\n      id: id,\n      UserId: req.user.id\n      }})\n  .then(\n    (alert) => detector.functionA(alert)\n  )\n  .then(\n    (alert) => detector.functionB(alert)\n  )\n});\n<\/pre>\n<p>This example shows how you can use the functions defined in the detector module as well (this is in an ExpressJS app on heroku)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to make a new CommonJS module for Node, you can use the following as a template file (we&#8217;ll call this &#8216;detector.js&#8217;, but obviously you can name it whatever you want): &#8220;use strict&#8221;; let _ = require(&#8216;lodash&#8217;); exports.functionA = (data) => { return data; }; exports.functionB = (data) => { return data; }; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/node-commonjs-example-library-template\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Node CommonJS Example Library Template&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[117,209,270,302,343,387,388,389,463,504],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2964"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2964"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2964\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}