Deno Tutorial #3 - Standard Library & Servers
Key Takeaways
Sets up a server and uses standard library modules in a Deno application
Full Transcript
all right then gang so far we've seen some of the basic built-in functionality in dino and to do this we've not needed to import any kind of external modules but dino also provides us with what's known as the standard library and you can find it at this address right here which i'll leave down below in the description now the standard library is basically a collection of modules for commonly used tasks and these modules are maintained by dino themselves and not third parties so you can always be sure that they're going to work with dino and they will be maintained now there's quite a lot of different modules in the standard library we're going to take a look at a few but it's also important to mention that not all of them are currently fully stable and in order to work with some of them we're going to have to explicitly use the unstable flag and we'll see that later on first of all let's try using this uuid module which is a nice addition to dino and it allows us to generate unique ids very easily so then to use this module we need to do an import like this and most of the time when you're using an import from one of these standard libraries it's going to be the mod file that we import but not always so let us grab this right here we're going to copy that and by the way if you click on most of these different packages right here you're going to see examples of how to use that down here so it's always good to have a read of those but anyway let's now go and paste this over here and what we're doing is importing something from this file right here and we're importing v4 so this is one of the versions to generate unique ids so now if we run this file it's going to download this package right here and it's going to cache it for our project so it stores it away on our computer but not inside a node modules folder over here but that's all we need to do use an import statement like this and now we can use this module in our code so to generate something a uid all we need to do is say const and then we'll call this uid you can call it what you want it really doesn't matter and then we'll use v4 which we just imported and a method on that called generate like so and that's all there is to it it generates us a unique uid and it's going to place that inside here so let's log that to the console console.log and uid so now let's give this a whirl save it and open up your terminal let me get rid of that junk from the last lesson so clear and now if i just make this a little smaller and then say dino run sandbox dot ts press enter and we're gonna see this unique id right here so that's what it's generated for us and every time we run this it's gonna be different so if i use the same command to run this again we're going to see a difference id right here pretty awesome right all right then so let's try using a different module this time the fs1 which stands for file system and this is going to give us a load of different methods that we can use to do different things with our file system more than we can do out of the box so if we click on that module and scroll down it should show us how to use this how to import it and basically we're just importing things from this file right here so let me just grab this copy that and i'm gonna paste it right here i'm gonna delete these two things because i don't want those two things and instead i'm gonna import two methods one of them is called readjason and the other one is called write.json so these are two methods that we can use to either read a json file on our computer or write a json file so let's use the first one which is read jason now to do that we need a json file so i'm going to just create a new file and call this ninjas.json press enter and i'm just going to paste in a load of json from my github file over here you can see it's just three objects we have a name property on each an age and a belt color so that's the json dead simple and now i just want to read that file that json from this file using this method right here read json so i'm going to say const json object that's what i'm storing the result in is equal to await because this read json method right here that is asynchronous so awaits read json and then inside we need to say what json file we want to read and it's ninjas dot json and then we just want to log the result to the console so console.log and it's going to be json all right dead simple right so let's give this a whirl i'm going to say dino run and then we need the allow hyphen read flag because we're reading files on our computer and then the name of this file which is sandbox dot ts and we're still gonna get an error and i'm gonna show you what that means in a second but if i try to run this we get all these errors and that's because this package this fs package at the minute is marked as unstable so by default it's not going to work but if we still want to play around with it we still want to use it all we need to do is pass this the unstable flag so i can say dash dash unstable like so and now if i run this it's going to work hopefully and we see the result right here so we get that array of objects back so that's a nice little method that we can use all right let's do something similar this time we want to write json so we need some data in this file in order to write that to a json file so i'm just going to create an array of objects so let's call this books and set it equal to an array and instead of you watching me type out a couple of objects i'm going to paste these from my repo and you can see it's just a couple of objects with a title and an author so i'm going to take that array and i want to write a json file based on that array and we're going to use this right json method to do it simple so we say await and then write json and then over here we pass in the file we want to create i'm going to call it books.json you can call it what you want and the data we want to write to that file is this thing right here that's the second argument so we can pass in a box like so now i'm going to just log under here console.log created books dot json so that when this runs we know that this is finished so let me save this again and let me run this again and this time we also need allow right because we're trying to write a file right here so let me pass that flag right here allow hyphen right and press enter and hopefully in a second we see createdbooks.json and we see that file right here if we go in we see all of that json but it's not formatted very well so what we could do is pass a third argument right here which is an options object and one of those is a spaces property and this allows us to format it a bit better and it's basically saying how many spaces do you want each indentation to be well i'm going to say 2 and press save and now if i try to run this again then we should see inside the box.json file it looks a bit nicer okay so that's pretty awesome there's two nice methods there to read jason and right jason but remember at the time of recording this it's currently unstable this module so we have to use this unstable flag for this to work at the minute okay so next up let's use the http module to create a server in dino all right so there's also a module in the deno standard library for doing things like creating a server and that's this module right here http so if we click on that it's going to show us down here how we can use this and i'm just going to grab this top line over here to import it and i'm going to paste this right over here so we're importing this thing right here serve from this file over here and this is actually a function and when we invoke it it creates a server for us so let's try that i'm going to say down here const serve is equal or rather we'll call this server is equal to serve and then we invoke this and then we pass in an object as an argument and the property inside this object we need to create for now is the port property and that's what port number we want to listen to so i'm just going to say 3000 so now if we were to run this it's going to create a server which is listening to port 3000 on localhost so under here i'm also going to log something to the console console.log and we'll just say listening for requests on port 3000 right dead simple so if i now run this by opening up the terminal and saying dino run and then we need to use the flag allow hyphen net because we're going to be using the network if we didn't do this and i'll demo that sandbox.cs we'll get an error so you see right here permission denied so we need to pass in that allow net flag so i'm going to do that right now dino run allow hyphen nets and then sandbox dot yes and now we can see we're listing for requests on port 3000 but if we go to port 3000 over here on localhost then nothing actually loads it's not working and that's because we're not actually doing anything with any requests that come in so it's not going to work yet and we need to listen for those requests and then respond to them so at the minute this is all kind of very similar to how node.js works we create a server using a different kind of method but then basically we're listening for requests but the way that we listen for requests and then respond to them in dino using this server is by using an asynchronous for in loop and that is different to how we typically do it in node so let me write this out first of all then i'm going to explain it so i'm going to say 4 awaits and then in parentheses const request of server and remember that's this thing right here and then inside this for loop i'm going to say request and then use a method in fact you know what i'm just going to console.log something for now to say request made right so what's happening here what does this all mean well the for a weight loop is basically an asynchronous loop and it allows us to iterate over an asynchronous iterable object now this server right here that is an asynchronous iterable object meaning that we can iterate over it like we could an array of items but this time it's an asynchronous iterable object meaning we don't have all the items ready to iterate over straight away so once this loop is first registered it essentially awaits request items to come into the server and then whenever a new request comes in we run the loop for that request and we get access to that request from that server okay so now if i save this and come back over here and refresh if i go to the console over here oops my mistake i need to run this first of all so now let me press up and enter to run this file now we're listening for request on port 3000 if i now come to refresh over here we should see request made right and if i stop and refresh again we should see another request made now we don't just want to do this we want to actually respond to the request and send something to the browser right so how do we do that well we can say request rec which is this object right here and this represents the request that comes in and then we can use a method on that called respond and that sends a response to the browser now inside here i'm going to pass in an object and then i'm going to specify what the body of this response should be and i'm just going to send back a string now i'm going to use a template string because in a minute i'm going to output a dynamic variable inside this but i'll say hello ninjas and then save it for now so if we now go over here in fact we need to cancel out of this process because we've made a change to this file so let me cancel out the process run the file again and now if we go over here and if we refresh we already saw it but if we refresh now we're getting this response back from our server so the request is coming in and it's handling that request by sending this response and the body of that response is this string which is coming back to the browser over here awesome now that we can get other information from this request object so i could get for example the url of the request so what i'm going to do is say const url is equal to request dots and then you see we have these different properties right here that we have access to i'm going to grab the url so whatever we visit up here if it was forward slash about then the url would be forward slash about and i'm gonna come down here and i'm gonna output this url inside this template string so i'm gonna say you visited and then the way we output a variable inside a template string is the dollar sign curly braces then the name of the variable so url in our case save that and come over here refresh and oops we can't do that because again we need to restart the server i often forget that so we need to say dino run allow net sandbox.ts then come over here and refresh and you can see hello ninjas you visited justforwardslash which is the root of the address but if we go to for example forward slash about then we can see forward slash about and this is going to be the same for whatever url that we go to forward slash about us and we can see forward slash about us right here so then you could if you wanted to evaluate this url on every request that comes in and then we could send a different response back to the browser for each different url that is requested and you could use this approach to set up some kind of api but there's also third-party packages to help us create dino apis and websites too so i'm going to be showing you how to use one of those in probably a couple of tutorials time but first of all let's learn about how to use third-party packages in general with dino in the next video
Original Description
Hey gang, in this Debo tutorial we'll take a look at a couple of modules from the standard library & also see how to set up a server.
--- Chapters ---
0:00 - uuid module
2:56 - fs module
7:48 - http module (server)
🐱👤🐱👤 JOIN THE GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join
----------------------------------------
🐱💻 🐱💻 My Udemy Courses:
+ Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript
+ Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase
+ D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase
🐱💻 🐱💻 Course Files:
https://github.com/iamshaunjp/deno-jumpstart
🐱💻 🐱💻 Other Related Free Courses:
+ Node.js Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9gcy9lrvMJ75z9maRw4byYp
+ TypeScript tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9gUgr39Q_yD6v-bSyMwKPUI
🐱💻 🐱💻 Deno links
+ install guide - https://deno.land/#installation
+ docs - https://deno.land/manual
+ chocolatey - https://chocolatey.org/install
+ std library - https://deno.land/std
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Frontend Performance
View skill →Related Reads
Chapters (3)
uuid module
2:56
fs module
7:48
http module (server)
🎓
Tutor Explanation
DeepCamp AI