{"id":1565,"date":"2016-06-22T08:26:14","date_gmt":"2016-06-22T08:26:14","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1565"},"modified":"2021-01-20T17:36:31","modified_gmt":"2021-01-20T17:36:31","slug":"node-js-diagram-module","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/node-js-diagram-module\/","title":{"rendered":"Node.js diagram module"},"content":{"rendered":"<p>MindFusion.Diagramming for JavaScript is now also available as a Node.js module, and you can use the diagram API you know and love in server code \ud83d\ude42 A sample server application and the module script are available here:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/_samples\/diagram_nodejs.zip\">diagram_nodejs.zip<\/a><\/p>\n<p>For example, you can submit to server a diagram drawn interactively by the user and examine its contents there by iterating over the nodes and links members of the Diagram class:<\/p>\n<pre>\/\/ on client side\n$.ajax(\n{\n\ttype: \"post\",\n\turl: \"http:\/\/localhost:1234\/diagram\", \n\tcontentType: \"application\/json\",\n\tdata: diagram.toJson(),\n\tsuccess: function(data)\n\t{\n\t\tconsole.log('success');\n\t},\n\terror: function(jqXHR, textStatus, err)\n\t{\n\t\tconsole.log(err);\n\t}\n});\n\n\/\/ on server side\napp.post('\/diagram', function(req, res)\n{\n    \/\/ won't be required in final release\n    var dummyCanvas = { parentNode:{} };\n\n    \/\/ create Diagram instance\n    var diagram = new Diagram(dummyCanvas);\n\n    \/\/ load diagram elements drawn by user\n    diagram.fromJson(req.rawBody);\n\n    \/\/ examine diagram contents\n    console.log(diagram.nodes.length + \" nodes\");\n    console.log(diagram.links.length + \" links\");\n    diagram.nodes.forEach(function (node, index)\n    {\n        console.log(\"node \" + index + \": \" + node.getText());\n    });\n\n    \/\/ send some response\n    res.send('ok');\n});\n<\/pre>\n<p>Or you could build the diagram on server side and send it to the browser to render in client-side Diagram control:<\/p>\n<pre>\/\/ on server side\napp.get('\/diagram', function(req, res)\n{\n    \/\/ won't be required in final release\n    var dummyCanvas = { parentNode:{} };\n\n    \/\/ create Diagram instance\n    var diagram = new Diagram(dummyCanvas);\n\n    \/\/ create some diagram items\n    var node1 = diagram.getFactory().createShapeNode(10, 10, 40, 30);\n    var node2 = diagram.getFactory().createShapeNode(60, 10, 40, 30);\n    var link = diagram.getFactory().createDiagramLink(node1, node2);\n\n    \/\/ set nodes' content\n    node1.setText(\"node.js\");\n    node1.setBrush(\"orange\");\n    node2.setText(\"hello there\");\n\n    \/\/ send diagram json\n    res.send(\n        diagram.toJson());\n});\n\n\/\/ on client side\n$.ajax(\n{\n\ttype: \"get\",\n\turl: \"http:\/\/localhost:1234\/diagram\", \n\tsuccess: function(data)\n\t{\n\t\tdiagram.fromJson(data);\n\t},\n\terror: function(jqXHR, textStatus, err)\n\t{\n\t\tconsole.log(err);\n\t}\n});\n<\/pre>\n<p>To run the sample Node.js application, run &#8220;node server.js&#8221; from command line and open http:\/\/localhost:1234\/client.html in your browser. Draw some nodes and links, edit their text and click Post to see them enumerated in Node&#8217;s console. Clicking the Get button will show this diagram built on server side:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/mindfusion.dev\/_samples\/nodejs_diagram.png\" alt=\"diagram built in node.js\" \/><\/p>\n<p>For more information on MindFusion&#8217;s JavaScript Diagram API, see <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm\">MindFusion.Diagramming online help<\/a><\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MindFusion.Diagramming for JavaScript is now also available as a Node.js module, and you can use the diagram API you know and love in server code \ud83d\ude42 A sample server application and the module script are available here: diagram_nodejs.zip For example, &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/node-js-diagram-module\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,74],"tags":[3,80,407],"class_list":["post-1565","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code","tag-diagram","tag-javascript","tag-node-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-pf","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1565"}],"version-history":[{"count":3,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":2592,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1565\/revisions\/2592"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}