{"id":542,"date":"2016-10-28T09:37:23","date_gmt":"2016-10-28T08:37:23","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=542"},"modified":"2022-07-25T08:35:46","modified_gmt":"2022-07-25T07:35:46","slug":"nodejs-webapi-express-js","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/nodejs-webapi-express-js\/","title":{"rendered":"NodeJS WebAPI (Express JS) CORS"},"content":{"rendered":"<p>Source: <a href=\"https:\/\/williambert.online\/2013\/06\/allow-cors-with-localhost-in-chrome\/\">https:\/\/williambert.online\/2013\/06\/allow-cors-with-localhost-in-chrome\/<\/a><\/p>\n<pre lang=\"javascript\">\r\n\r\napp.all(\"\/api\/*\", function(req, res, next) {\r\n  res.header(\"Access-Control-Allow-Origin\", \"*\");\r\n  res.header(\"Access-Control-Allow-Headers\", \"Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With\");\r\n  res.header(\"Access-Control-Allow-Methods\", \"GET, PUT, POST\");\r\n  return next();\r\n});\r\n\r\napp.all(\"\/api\/*\", function(req, res, next) {\r\n  if (req.method.toLowerCase() !== \"options\") {\r\n    return next();\r\n  }\r\n  return res.send(204);\r\n});\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/williambert.online\/2013\/06\/allow-cors-with-localhost-in-chrome\/ app.all(&#8220;\/api\/*&#8221;, function(req, res, next) { res.header(&#8220;Access-Control-Allow-Origin&#8221;, &#8220;*&#8221;); res.header(&#8220;Access-Control-Allow-Headers&#8221;, &#8220;Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With&#8221;); res.header(&#8220;Access-Control-Allow-Methods&#8221;, &#8220;GET, PUT, POST&#8221;); return next(); }); app.all(&#8220;\/api\/*&#8221;, function(req, res, next) { if (req.method.toLowerCase() !== &#8220;options&#8221;) { return next(); } return res.send(204); });<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4],"tags":[],"class_list":["post-542","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=542"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":545,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/542\/revisions\/545"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}