{"id":1092,"date":"2020-09-27T16:50:54","date_gmt":"2020-09-27T16:50:54","guid":{"rendered":"https:\/\/www.datatype.co.in\/blog\/?p=1092"},"modified":"2020-10-11T17:23:24","modified_gmt":"2020-10-11T17:23:24","slug":"getting-started-with-deno","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/getting-started-with-deno\/","title":{"rendered":"Getting Started With Deno"},"content":{"rendered":"\n<p>In this article, I am going to show you how to install Deno and write a simple Deno program and execute it, from scratch. We will be developing basic deno http server which will listen to a predefined port and respond. Let&#8217;s get started.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Installing Deno on Windows:<\/h4>\n\n\n\n<p>Open Windows Power Shell and run the following command &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>iwr https:\/\/deno.land\/x\/install\/install.ps1 -useb | iex<\/code><\/pre>\n\n\n\n<p>If you are Mac or Linux user, in the shell execute the following command &#8211; <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -fsSL https:\/\/deno.land\/x\/install\/install.sh | sh<\/code><\/pre>\n\n\n\n<p>This will install Deno in your machine, you will see the following output in the power shell window-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS C:\\Users> iwr https:\/\/deno.land\/x\/install\/install.ps1 -useb | iex\nDeno was installed successfully to C:\\Users\\Lok\\.deno\\bin\\deno.exe\nRun 'deno --help' to get started<\/code><\/pre>\n\n\n\n<p>Now check the installed Deno version by running <code><strong>deno --version<\/strong><\/code> command. If it is installed correctly, it will output the version as shown below-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS C:\\Users> deno --version\ndeno 1.4.2\nv8 8.7.75\ntypescript 4.0.3<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"686\" height=\"67\" src=\"https:\/\/www.datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-18.png\" alt=\"Deno Version Check\" class=\"wp-image-1106\" srcset=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-18.png 686w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-18-300x29.png 300w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/figure>\n\n\n\n<p>Now, let&#8217;s try running a sample Deno program. Run the following command on Windows Power Shell.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS C:\\Users> deno run https:\/\/deno.land\/std\/examples\/welcome.ts<\/code><\/pre>\n\n\n\n<p>It will output <strong>Welcome to Deno<\/strong> as shown below-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS C:\\Users> deno run https:\/\/deno.land\/std\/examples\/welcome.ts\n\u2190[0m\u2190[32mDownload\u2190[0m https:\/\/deno.land\/std\/examples\/welcome.ts\n\u2190[0m\u2190[33mWarning\u2190[0m Implicitly using latest version (0.71.0) for https:\/\/deno.land\/std\/examples\/welcome.ts\n\u2190[0m\u2190[32mDownload\u2190[0m https:\/\/deno.land\/std@0.71.0\/examples\/welcome.ts\n\u2190[0m\u2190[32mCheck\u2190[0m https:\/\/deno.land\/std@0.71.0\/examples\/welcome.ts\nWelcome to Deno \ud83e\udd95<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Creating Simple Deno Server:<\/h4>\n\n\n\n<p>Now, let us create a simple Deno server app. Create a directory deno-app, add a new file say, <strong>server.ts <\/strong>and write the following code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/server.ts\nimport { serve } from \"https:\/\/deno.land\/std@0.71.0\/http\/server.ts\";\nconst s = serve({ port: 8000 });\nconsole.log(\"http:\/\/localhost:8000\/\");\nfor await (const req of s) {\n  req.respond({ body: \"Hello world, this is my first Deno app! \" });\n}<\/code><\/pre>\n\n\n\n<p>Execute the code by running <strong><code>deno run --allow-net server.ts<\/code><\/strong> command-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PS E:\\Deno\\deno-app> deno run --allow-net server.ts\n\u2190[0m\u2190[32mCheck\u2190[0m file:\/\/\/E:\/Deno\/deno-app\/server.ts\nhttp:\/\/localhost:8000\/<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"746\" height=\"109\" src=\"https:\/\/www.datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-17.png\" alt=\"Run Deno App\" class=\"wp-image-1104\" srcset=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-17.png 746w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-17-300x44.png 300w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-17-710x104.png 710w\" sizes=\"auto, (max-width: 746px) 100vw, 746px\" \/><\/figure>\n\n\n\n<p>That&#8217;s all. Now, our Deno server is up and it is listening to port 8000. You can open a browser and browse the URL <strong><code>http:\/\/localhost:8000\/<\/code><\/strong>, you will see the following output- <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"498\" height=\"149\" src=\"https:\/\/www.datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-20.png\" alt=\"Running Deno App\" class=\"wp-image-1111\" srcset=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-20.png 498w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/image-20-300x90.png 300w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/figure>\n\n\n\n<p>Great! We have successfully developed a basic http server using Deno. As you see, whatever you type after <strong>localhost:8000<\/strong>, it will always return same <strong>Hello world<\/strong> message, because we have not written any code to handle different requests. In the upcoming article, I will show you how to handle different requests and respond back with data.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">List of Permission flag<\/h4>\n\n\n\n<p>As deno is secure by default it doesn\u2019t have any permission enable. These flags will be useful while for permission while running a file.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>\u2014 allow-env:<\/strong><\/code>Allow environment access.<\/li><li><code><strong>\u2014 allow-hrtime:<\/strong><\/code>Allow high-resolution time measurement.<\/li><li><code><strong>\u2014 allow-net:<\/strong><\/code>Allow network access.<\/li><li><code><strong>\u2014 allow-plugin:<\/strong><\/code>Allow loading plugins.<\/li><li><code><strong>\u2014 allow-read:<\/strong><\/code>Allow file system read access.<\/li><li><code><strong>\u2014 allow-run:<\/strong><\/code>Allow running subprocesses.<\/li><li><code><strong>\u2014 allow-write:<\/strong><\/code>Allow file system write access.<\/li><li><code><strong>\u2014 allow-all:<\/strong><\/code><strong>:&nbsp;<\/strong>Give all access.<\/li><\/ul>\n\n\n\n<p> Hope this helps. Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I am going to show you how to install Deno and write a simple Deno program and execute it, from scratch. We will be developing basic deno&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[250],"tags":[256,259,261,260],"class_list":["post-1092","post","type-post","status-publish","format-standard","hentry","category-deno","tag-deno-example","tag-deno-getting-started","tag-deno-sample-code","tag-install-deno-in-windows","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1092"}],"version-history":[{"count":23,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1092\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1092\/revisions\/1128"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}