﻿
{"id":9162,"date":"2018-08-27T11:22:06","date_gmt":"2018-08-27T11:22:06","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=9162"},"modified":"2018-08-27T11:22:06","modified_gmt":"2018-08-27T11:22:06","slug":"node-jsde-body-parser-modulu-ile-post-edilen-datayi-yakalamak","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/node-jsde-body-parser-modulu-ile-post-edilen-datayi-yakalamak\/","title":{"rendered":"Node.js&#8217;de Body-Parser Mod\u00fcl\u00fc \u0130le Post Edilen Datay\u0131 Yakalamak"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Takip eden okuyucular\u0131m bilir ki, \u015fu ana kadar Node.js&#8217;de sadece get i\u015flemleri ger\u00e7ekle\u015ftirdi\u011fimiz \u00e7al\u0131\u015fmalarla makaleleri \u00f6rneklendirmi\u015f bulunmaktay\u0131z. Dolay\u0131s\u0131yla bu i\u00e7eri\u011fimizde kullan\u0131c\u0131lar taraf\u0131ndan post edilen datan\u0131n Body-Parser mod\u00fcl\u00fc ile nas\u0131l yakaland\u0131\u011f\u0131n\u0131 inceleyecek ve pratik olarak konuyu ele alaca\u011f\u0131z.<\/p>\n<p>\u0130lk olarak Body-Parser mod\u00fcl\u00fcn\u00fc projemize entegre ederek ba\u015flamal\u0131y\u0131z. Tabi \u00f6ncelikle bu mod\u00fcl\u00fcn ne oldu\u011fundan bahsetmekte fayda var. Body-Parser mod\u00fcl\u00fc; g\u00f6nderilen post datas\u0131n\u0131 obje olarak yakalamam\u0131z\u0131 sa\u011flayan bir mod\u00fcld\u00fcr. Asl\u0131nda kullan\u0131c\u0131 taraf\u0131ndan ilgili datay\u0131 bize g\u00f6nderme g\u00f6revini icra etmekle y\u00fck\u00fcml\u00fc desek daha do\u011fru ifade etmi\u015f oluruz.<\/p>\n<p>\u0130lgili mod\u00fcl\u00fc projeye entegre etmek i\u00e7in;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nnpm install body-parser --save\r\n<\/pre>\n<p>komutunu \u00e7al\u0131\u015ft\u0131rman\u0131z yeterlidir.<\/p>\n<p>Ard\u0131ndan projenin ana mod\u00fcl\u00fc olan &#8220;app.js&#8221; dosyas\u0131nda Body-Parser mod\u00fcl\u00fcn\u00fc require ederek a\u015fa\u011f\u0131daki gibi gerekli \u00e7al\u0131\u015fmalar\u0131 ger\u00e7ekle\u015ftirmemiz gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst express = require(&quot;express&quot;);\r\nconst app = express();\r\nconst bodyParser = require(&quot;body-parser&quot;);\r\n\r\nrequire(&quot;.\/db\/db&quot;)();\r\n\r\napp.use(bodyParser.json());\r\napp.use(bodyParser.urlencoded({ extended: true }));\r\n\r\nconst employeeRouter = require(&quot;.\/routers\/employeeRouter&quot;);\r\napp.use(&quot;\/employee&quot;, employeeRouter);\r\n\r\napp.listen(5000, () =&gt; console.log(&quot;Yay\u0131n ba\u015flad\u0131.&quot;));\r\n<\/pre>\n<p>Burada yap\u0131lan ayarlar\u0131 izah edersek e\u011fer;<\/p>\n<ul>\n<li>\n<strong>app.use(bodyParser.json())<\/strong><br \/>\nJSON veri tipinde gelecek olan datalar\u0131n kullan\u0131labilmesi i\u00e7in bu tan\u0131mlaman\u0131n yap\u0131lmas\u0131 gerekmektedir.\n<\/li>\n<li>\n<strong>app.use(bodyParser.urlencoded({ extended: true }))<\/strong><br \/>\nEncode(kodlanm\u0131\u015f\/\u015fifrelenmi\u015f) edilmi\u015f urller \u00fczerinde Body-Parser&#8217;\u0131 kullanmak istiyorsan\u0131z e\u011fer &#8220;extended&#8221; \u00f6zelli\u011fine true de\u011ferini vermeniz yeterlidir.\n<\/li>\n<\/ul>\n<p>Ana mod\u00fclde(app.js) Body-Parser mod\u00fcl\u00fc ile gerekli bu ayarlamalar\u0131 yapt\u0131ktan sonra route rotas\u0131nda a\u015fa\u011f\u0131daki \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirilebilir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst express = require(&quot;express&quot;);\r\nconst router = express.Router();\r\n\r\nrouter.post(&quot;\/postEmployee&quot;, (request, response, next) =&gt; {\r\n    const { name, surName } = request.body;\r\n    response.send(`Name : ${name} - Surname : ${surName}`);\r\n});\r\n\r\nmodule.exports = router;\r\n<\/pre>\n<p>Route yap\u0131lanmas\u0131nda g\u00f6rd\u00fc\u011f\u00fcn\u00fcz \u00fczere &#8220;post&#8221; fonksiyonu tetiklendi\u011fi anda &#8220;request&#8221; parametresinin &#8220;body&#8221; \u00f6zelli\u011fi bize post edilen datay\u0131 json olan getirecektir. Biz <a href=\"http:\/\/www.gencayyildiz.com\/blog\/ecmascript-6-destructuring\/\" rel=\"noopener\" target=\"_blank\">Destructuring<\/a> y\u00f6ntemi ile ilgili data i\u00e7erisindeki alanlar\u0131 &#8220;name&#8221; ve &#8220;surName&#8221; alanlar\u0131na atam\u0131\u015f bulunmaktay\u0131z.<\/p>\n<p>Dolay\u0131s\u0131yla Postman uygulamas\u0131yla ilgili adrese post talebinde bulunursak e\u011fer;<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/08\/Node.jsde-Body-Parser-Mod\u00fcl\u00fc-\u0130le-Post-Edilen-Datay\u0131-Yakalamak.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/08\/Node.jsde-Body-Parser-Mod\u00fcl\u00fc-\u0130le-Post-Edilen-Datay\u0131-Yakalamak.png\" alt=\"Node.js&#039;de Body-Parser Mod\u00fcl\u00fc \u0130le Post Edilen Datay\u0131 Yakalamak\" width=\"430\" height=\"401\" class=\"aligncenter size-full wp-image-9176\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/08\/Node.jsde-Body-Parser-Mod\u00fcl\u00fc-\u0130le-Post-Edilen-Datay\u0131-Yakalamak.png 430w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/08\/Node.jsde-Body-Parser-Mod\u00fcl\u00fc-\u0130le-Post-Edilen-Datay\u0131-Yakalamak-300x280.png 300w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/a><br \/>\nekran al\u0131nt\u0131s\u0131nda da g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere alanlara girilen de\u011ferler elde edilebilmektedir.<\/p>\n<p>Bu noktadan itibaren post neticesinde yakalanan bu datay\u0131 ister veritaban\u0131na ekleyebilirsiniz, ister mail g\u00f6nderebilirsiniz isterseniz de farkl\u0131 algoritmik i\u015flemlerde kullanabilirsiniz. Yani bundan sonras\u0131 size kalm\u0131\u015f \ud83d\ude42<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Takip eden okuyucular\u0131m bilir ki, \u015fu ana kadar Node.js&#8217;de sadece get i\u015flemleri ger\u00e7ekle\u015ftirdi\u011fimiz \u00e7al\u0131\u015fmalarla makaleleri \u00f6rneklendirmi\u015f bulunmaktay\u0131z. Dolay\u0131s\u0131yla bu i\u00e7eri\u011fimizde kullan\u0131c\u0131lar taraf\u0131ndan post edilen datan\u0131n Body-Parser mod\u00fcl\u00fc ile nas\u0131l yakaland\u0131\u011f\u0131n\u0131 inceleyecek ve pratik&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":8600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2145],"tags":[2585,2584,2586,2587],"class_list":["post-9162","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js","tag-body-parser","tag-body-parser-modulu","tag-node-js-body-parser-modulu","tag-node-jsde-body-parser"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=9162"}],"version-history":[{"count":15,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9162\/revisions"}],"predecessor-version":[{"id":9178,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/9162\/revisions\/9178"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/8600"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=9162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=9162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=9162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}