{"id":2952,"date":"2015-10-28T23:03:43","date_gmt":"2015-10-28T19:03:43","guid":{"rendered":"http:\/\/nayarweb.com\/blog\/?p=2952"},"modified":"2015-11-05T21:10:37","modified_gmt":"2015-11-05T17:10:37","slug":"wtf-express-just-give-me-my-body-nodejs","status":"publish","type":"post","link":"https:\/\/nayarweb.com\/blog\/2015\/wtf-express-just-give-me-my-body-nodejs\/","title":{"rendered":"WTF Express! Just give me my body. #NodeJS"},"content":{"rendered":"<p>Express is a web framework for NodeJS like what Slim Framework, Free Free Framework is to PHP. The idea is simple. Minimalist frameworks don&#8217;t do much work except for routing requests.<\/p>\n<p>For example, if you want when someone access \/ on your website, a text saying &#8220;Hello World&#8221; appears, you do it this way:<\/p>\n<pre>var http = require('http');\r\nvar express = require('express');\r\nvar app = express();\r\n\r\napp.get('\/', function(req, res) {\r\n  res.send(\"Hello World\");\r\n});\r\n\r\nvar server = app.listen(3000, function () {});\r\n<\/pre>\n<p>If you want to do \/hello\/{a name here}:<\/p>\n<pre>app.get('\/hello\/:soz', function(req, res) {\r\n  res.send(\"Hello \" + req.params.soz);\r\n});\r\n<\/pre>\n<p>In the above examples, we have used HTTP <em>GET<\/em> method. There is a limit in the amount of data we can put in an HTTP header. The <em>POST<\/em> method is used to send longer data. I will leave the discussion when to use GET, POST and other HTTP verbs in another blog post. Lemme show you how to make a POST request in AJAX using jQuery and retrieve the data in NodeJS.<\/p>\n<p>Client-side\/browser-side:<\/p>\n<pre>\r\n$.ajax({\r\n  method: \"post\",\r\n  url: \"\/api\/dimoune\/\",\r\n  data: {\r\n    name: \"Nayar\"\r\n  },\r\n  success: function( data ) {\r\n    console.log(data);\r\n  }\r\n});\r\n<\/pre>\n<p>Server-side:<\/p>\n<pre>\r\nvar bodyParser = require('body-parser');\r\napp.use(bodyParser.urlencoded({extended: true }));\r\n\r\napp.post('\/api\/dimoune\/', function(req,res){\r\n  \/\/ Logic to save to database or something \r\n  res.send(\"saved \" + req.body.name);\r\n});\r\n<\/pre>\n<p>Notice I had to add <em>body-parser<\/em> library. Without it, req.body would be not be defined thus you would not be able to access your posted data. Kinda stupid to not include it by default in Express no?<\/p>\n<p><strong>UPDATE:<\/strong><br \/>\nAnother big headache. For posting data with curl command e.g.<\/p>\n<pre>\r\ncurl -H \"Content-Type:application\/json\" -X POST --data '{\"name\":\"nayar\",\"sex\":\"male\"}' http:\/\/example.com\/api\/dimounes\r\n<\/pre>\n<p>You need to add the following lines in your nodejs file<\/p>\n<pre>app.use(bodyParser.json());<\/pre>\n<p>Refs:<br \/>\nhttps:\/\/stackoverflow.com\/a\/18808957\/4609496<br \/>\nhttp:\/\/stackoverflow.com\/a\/25471936<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"GITx7UoHiT\"><p><a href=\"https:\/\/api.jquery.com\/jQuery.post\/\">jQuery.post()<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;jQuery.post()&#8221; &#8212; jQuery API Documentation\" src=\"https:\/\/api.jquery.com\/jQuery.post\/embed\/#?secret=ctXnkyGHny#?secret=GITx7UoHiT\" data-secret=\"GITx7UoHiT\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Express is a web framework for NodeJS like what Slim Framework, Free Free Framework is to PHP. The idea is simple. Minimalist frameworks don&#8217;t do much work except for routing requests. For example, if you want when someone access \/ on your website, a text saying &#8220;Hello World&#8221; appears, you do it this way: var &hellip; <a href=\"https:\/\/nayarweb.com\/blog\/2015\/wtf-express-just-give-me-my-body-nodejs\/\" class=\"continue-reading\">Continue reading <span class=\"screen-reader-text\">WTF Express! Just give me my body. #NodeJS<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[210,1],"tags":[215,84],"class_list":["post-2952","post","type-post","status-publish","format-standard","hentry","category-technology","category-uncategorized","tag-nodejs","tag-programming"],"_links":{"self":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/2952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/comments?post=2952"}],"version-history":[{"count":8,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/2952\/revisions"}],"predecessor-version":[{"id":2982,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/2952\/revisions\/2982"}],"wp:attachment":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/media?parent=2952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/categories?post=2952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/tags?post=2952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}