{"id":111365,"date":"2020-04-10T08:00:00","date_gmt":"2020-04-10T08:00:00","guid":{"rendered":"https:\/\/fedoramagazine.org\/?p=30805"},"modified":"2020-04-10T08:00:00","modified_gmt":"2020-04-10T08:00:00","slug":"using-fedora-to-quickly-implement-rest-api-with-javascript","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/04\/10\/using-fedora-to-quickly-implement-rest-api-with-javascript\/","title":{"rendered":"Using Fedora to quickly implement REST API with JavaScript"},"content":{"rendered":"<p>Fedora Workstation uses GNOME Shell by default and this one was mainly written in JavaScript. JavaScript is famous as a language of front-end development but this time we&#8217;ll show its usage for back-end.<\/p>\n<p>We&#8217;ll implement a new API using the following technologies: JavaScript, Express and Fedora Workstation. A web browser is being used to call the service (eg. Firefox from the default Fedora WS distro).<\/p>\n<p> <span id=\"more-30805\"><\/span> <\/p>\n<h2>Installing of necessary packages<\/h2>\n<p><strong>Check: What&#8217;s already installed?<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">$ npm -v\n$ node -v<\/pre>\n<p>You may already have both the necessary packages installed and can skip the next step. If not, install <em>nodejs<\/em>:<\/p>\n<pre class=\"wp-block-preformatted\">$ sudo dnf install nodejs<\/pre>\n<h2>A new simple service (low-code style)<\/h2>\n<p>Let\u2018s navigate to our working directory (<em>work<\/em>) and create a new directory for our new sample back-end app.<\/p>\n<pre class=\"wp-block-preformatted\">$ cd work\n$ mkdir newApp\n$ cd newApp\n$ npx express-generator<\/pre>\n<p>The above command generates an application skeleton for us.<\/p>\n<pre class=\"wp-block-preformatted\">$ npm i<\/pre>\n<p>The above command installs dependencies. Please mind the security warnings \u2013 never use this one for production.<\/p>\n<p>Crack open the <strong>routes\/users.js<\/strong><\/p>\n<p>Modify line #6 to: <\/p>\n<pre class=\"wp-block-preformatted\">res.send(data);<\/pre>\n<p>Insert this code block below <em>var router<\/em>:<\/p>\n<pre class=\"wp-block-preformatted\">let data = { '1':'Ann', '2': 'Bruno', '3': 'Celine' }<\/pre>\n<p>Save<br \/>\nthe modified file.<\/p>\n<p>We modified a route and added a new variable data. This one could be declared as a <em>const<\/em> as we didn\u2018t modify it anywhere. The result:<\/p>\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2020\/04\/using-fedora-to-quickly-implement-rest-api-with-javascript.png\" alt=\"\" class=\"wp-image-30806\" width=\"580\" height=\"435\" \/><\/figure>\n<h2>Running the service on your local Fedora workstation machine<\/h2>\n<pre class=\"wp-block-preformatted\">$ npm start<\/pre>\n<p><em>Note: The application entry point is bin\/www. You may want to change the port number there.<\/em><\/p>\n<h2>Calling our new service<\/h2>\n<p>Let\u2018s launch our Firefox browser and type-in:<\/p>\n<p><em>http:\/\/localhost:3000\/users<\/em><\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.sickgaming.net\/blog\/wp-content\/uploads\/2020\/04\/using-fedora-to-quickly-implement-rest-api-with-javascript-1.png\" alt=\"\" class=\"wp-image-30808\" \/><figcaption>Output<\/figcaption><\/figure>\n<p>It\u2018s also possible to leverage the Developer tools. Hit <strong>F12<\/strong> and in the <em>Network<\/em> tab, select the related GET request and look at the side bar response tab to check the data.<\/p>\n<h2>Conclusion<\/h2>\n<p>Now we have got a service and and an unnecessary index accessible through <em>localhost:3000<\/em>. To get quickly rid of this:<\/p>\n<ol>\n<li>Remove the <em>views<\/em> directory<\/li>\n<li>Remove the <em>public<\/em> directory<\/li>\n<li>Remove the <em>routes\/index.js<\/em> file<\/li>\n<li>Inside the <em>app.js<\/em> file, modify the line 37 to:<br \/> \n<div class=\"codecolorer-container text default\" style=\"overflow:auto;border:1px solid #9F9F9F;width:435px\">\n<div class=\"text codecolorer\" style=\"padding:5px;font:normal 12px\/1.4em Monaco, Lucida Console, monospace\">res.status(err.status || 500).end();<\/div>\n<\/div>\n<\/li>\n<li>Remove the next line <em>res.render(&#8216;error&#8217;)<\/em><\/li>\n<\/ol>\n<p>Then restart the service:<\/p>\n<pre class=\"wp-block-preformatted\">$ npm start<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fedora Workstation uses GNOME Shell by default and this one was mainly written in JavaScript. JavaScript is famous as a language of front-end development but this time we&#8217;ll show its usage for back-end. We&#8217;ll implement a new API using the following technologies: JavaScript, Express and Fedora Workstation. A web browser is being used to call [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":111366,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[971,972,45,973,42,43,974,46,47],"class_list":["post-111365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fedora-os","tag-api","tag-back-end","tag-fedora","tag-fedora-workstation","tag-for-developers","tag-for-system-administrators","tag-javascript","tag-magazine","tag-news"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/111365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=111365"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/111365\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/111366"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=111365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=111365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=111365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}