Coding

Node.js Tutorial for Beginners – Getting Started with NodeJS Basics

  • 00:00:09 so let's dive into the most important
  • 00:00:12 question what is no t.j.s no js' is a
  • 00:00:16 JavaScript runtime and now what does
  • 00:00:19 this mean
  • 00:00:19 you know JavaScript it's a programming
  • 00:00:23 language you typically use in the
  • 00:00:25 browser to manipulate your Dom to
  • 00:00:29 manipulate the page which was loaded in
  • 00:00:31 the browser for example to open a pop-up
  • 00:00:34 a modal or add any kinds of effects
  • 00:00:37 because javascript is a language to try
  • 00:00:39 this in the browser that allows you to
  • 00:00:41 interact with the page after it was
  • 00:00:43 loaded and therefore a secte rouge apart
  • 00:00:46 when it comes to building interactive
  • 00:00:48 user interfaces in the browser so
  • 00:00:50 whatever your users see however
  • 00:00:53 javascript is not limited to that no
  • 00:00:56 chess is a different version of
  • 00:00:59 JavaScript you could say it is basically
  • 00:01:02 built on JavaScript it adds some
  • 00:01:05 features to it is not capable of doing
  • 00:01:07 some other things you can do with
  • 00:01:08 JavaScript in a browser so it basically
  • 00:01:10 takes JavaScript and puts it into a
  • 00:01:12 different environment it allows you to
  • 00:01:15 run JavaScript code on the server you
  • 00:01:18 could say in theory not just on the
  • 00:01:20 server but on any machine though so it
  • 00:01:23 basically allows you to run JavaScript
  • 00:01:25 not just in the browser but anywhere
  • 00:01:28 else like a normal programming language
  • 00:01:30 like normal programs on your computer or
  • 00:01:33 some computer in the Internet
  • 00:01:35 effectively making it a great choice for
  • 00:01:37 building web applications that run on
  • 00:01:40 servers which are just computers running
  • 00:01:43 somewhere in the internet so in detail
  • 00:01:46 this means that we can use nodejs
  • 00:01:49 to run JavaScript outside of the browser
  • 00:01:52 that is the core takeaway now how does
  • 00:01:54 this work technically well no js' uses
  • 00:01:58 we ate and we ate simply it's the name
  • 00:02:01 off the JavaScript engine built by
  • 00:02:04 Google that runs JavaScript in the
  • 00:02:08 browser so back to the browser we now
  • 00:02:10 are we a to simply the name the
  • 00:02:12 creator's gave their engine and what
  • 00:02:15 does an engine
  • 00:02:16 well it simply means that engine takes
  • 00:02:19 javascript code the code running in your
  • 00:02:21 browser then or in nodes case which
  • 00:02:24 builds up on v8 although the nodejs
  • 00:02:26 JavaScript code it takes that JavaScript
  • 00:02:29 code and compiles it to machine code and
  • 00:02:31 this is what your browser does to what
  • 00:02:34 we ate does in your browser it does take
  • 00:02:37 your JavaScript code and compile to
  • 00:02:39 machine code because dad is the code
  • 00:02:41 that runs ultimately on your computer
  • 00:02:44 and that can be handled efficiently now
  • 00:02:47 this is done by v8 v8 itself is written
  • 00:02:51 in C++ but that doesn't really matter
  • 00:02:53 too much for you you don't need to write
  • 00:02:55 any C++ code to use JavaScript or
  • 00:02:58 node.js but no trace basically takes
  • 00:03:01 that v8 code bases which is written in
  • 00:03:03 C++ and adds certain features like for
  • 00:03:07 example working with your local file
  • 00:03:09 system opening files reading files
  • 00:03:12 deleting files these are all things
  • 00:03:14 which are not possible in the browser
  • 00:03:16 you can't access your local file system
  • 00:03:18 in the browser for security reasons so
  • 00:03:20 this is not supported nodejs adds these
  • 00:03:23 features to v8 engine you could say so
  • 00:03:26 that you can suddenly do that now no
  • 00:03:28 chance does not run in the browser so
  • 00:03:30 these restrictions still apply there you
  • 00:03:33 use vanilla we ate so we ate only
  • 00:03:36 without D node.js extensions but if you
  • 00:03:39 then install node trace you can use it
  • 00:03:42 to basically use that extended v8
  • 00:03:44 version to run JavaScript scripts on
  • 00:03:48 your computer which then suddenly can
  • 00:03:51 access these new features because they
  • 00:03:53 don't run in the browser but are
  • 00:03:55 directly executed through that no trace
  • 00:03:57 runtime you could say so this is how
  • 00:04:00 that works together and what noches does
  • 00:04:03 it allows you to run JavaScript on your
  • 00:04:05 computer and it adds useful
  • 00:04:07 functionalities to the JavaScript engine
  • 00:04:09 so that you can do more useful stuff
  • 00:04:12 there than you can do with browser side
  • 00:04:15 JavaScript now one important note maybe
  • 00:04:17 on this point all this but of course
  • 00:04:19 some features are also taken away in the
  • 00:04:22 browser you use JavaScript to interact
  • 00:04:24 with the document object model so with
  • 00:04:26 the HTML elements on your page if you
  • 00:04:29 just executed
  • 00:04:30 javascript file directly you of course
  • 00:04:32 have no attached page and therefore
  • 00:04:34 these features are missing but this is a
  • 00:04:37 lot of theory why don't we just have a
  • 00:04:40 look at this and see how we can install
  • 00:04:43 and use no trace and what it actually
  • 00:04:45 does for us let's do that in the next
  • 00:04:47 lecture
  • 00:04:51 so we want to use nodejs and the first
  • 00:04:55 important step is that we install it and
  • 00:04:57 for that you have to is it no js' dot or
  • 00:05:01 ik there you find an installer in my
  • 00:05:04 case here had picked the right one for
  • 00:05:06 my operating system Mac OS but you will
  • 00:05:09 also find a version for Windows if
  • 00:05:11 you're visiting this page on windows or
  • 00:05:13 you manually choose a different version
  • 00:05:15 and you can of course follow along no
  • 00:05:18 matter which operating system you're
  • 00:05:20 using the code you write and the steps
  • 00:05:22 you need to take to install no chairs
  • 00:05:24 are exactly the same you simply download
  • 00:05:27 the latest version here and you then
  • 00:05:30 simply save it anywhere and run the
  • 00:05:33 Installer this gives you now simply
  • 00:05:35 follow through all the steps which are
  • 00:05:37 mentioned in installer accept the
  • 00:05:39 License Agreement and so on so simply
  • 00:05:41 click through that install it on your
  • 00:05:44 machine and once this completed you
  • 00:05:47 already got everything you will need to
  • 00:05:49 write nodejs code so this is super fast
  • 00:05:52 it's done here you can close this now
  • 00:05:54 and now how do we use no chairs well the
  • 00:05:59 easiest way of using it is to open up
  • 00:06:01 your local terminal or on Windows
  • 00:06:04 PowerShell or the command prompt in its
  • 00:06:07 terminal as a first step simply run node
  • 00:06:11 dash V like this this should show you
  • 00:06:15 the version of nodejs you installed now
  • 00:06:18 with that being executed you can now use
  • 00:06:22 node just like that to enter a special
  • 00:06:25 input mode the so-called rebels I'll
  • 00:06:28 come back to that where you can execute
  • 00:06:30 code and have it parsed by node on the
  • 00:06:32 fly you can use this as a calculator
  • 00:06:35 simply type two plus two to see the
  • 00:06:37 result or you can write whole JavaScript
  • 00:06:39 programs there now I won't do that so
  • 00:06:42 you can quit it with control C and
  • 00:06:44 instead let's write a real script which
  • 00:06:47 we can execute with no trace so that we
  • 00:06:49 can also see what we can do with it for
  • 00:06:51 that create a new folder anywhere on
  • 00:06:54 your computer and open that folder in
  • 00:06:57 your favorite IDE or code editor now
  • 00:07:01 here I am using Visual Studio code you
  • 00:07:04 can find that
  • 00:07:05 on code dot we'll studio.com this is a
  • 00:07:08 great ide which i strongly recommend
  • 00:07:10 using it's free it is really built for
  • 00:07:13 you such with JavaScript code it will
  • 00:07:16 work great and you can simply download
  • 00:07:18 it from this page as I said for free
  • 00:07:21 simply walk through the installer it's
  • 00:07:23 always available for Windows and so on
  • 00:07:25 walk through the Installer it gives you
  • 00:07:27 and then once it is installed simply
  • 00:07:29 start it and open that new folder you
  • 00:07:32 created somewhere on your computer
  • 00:07:35 this will be your project folder for
  • 00:07:37 this very first quick app or script
  • 00:07:40 we're going to right now your Visual
  • 00:07:42 Studio code might look a bit different
  • 00:07:43 I'm in full screen mode which is why you
  • 00:07:45 don't see any menu items here at the top
  • 00:07:47 and I also got two important things
  • 00:07:50 installed here the first one is the
  • 00:07:53 theme and you can hit command shift P or
  • 00:07:56 ctrl shift P on Windows to go to that
  • 00:07:58 quick command interface here and there
  • 00:08:01 if you type theme then you can go to the
  • 00:08:05 Preferences color theme and choose which
  • 00:08:07 theme you want to use and I'm using the
  • 00:08:09 dark blaspheme here so if you want to
  • 00:08:11 get the exact same coloring you can
  • 00:08:12 choose that and I also installed an
  • 00:08:15 extension you can view the extension
  • 00:08:18 menu by going to view extensions or if
  • 00:08:21 you have on the left a menu you can also
  • 00:08:25 click on the exchange extensions tab
  • 00:08:27 there but view extensions will do or the
  • 00:08:29 shortcut you see there and there I
  • 00:08:31 installed the material I can theme this
  • 00:08:35 theme here I already got that installed
  • 00:08:38 and this gives me some special file
  • 00:08:39 icons and so on you don't need that you
  • 00:08:41 just can use it if you want to have the
  • 00:08:43 exact same layout and thereafter you can
  • 00:08:45 go back to the view Explorer here now
  • 00:08:48 this is then my set up I gotta get
  • 00:08:50 ignore file here since I'll use git for
  • 00:08:52 a reversion management you don't need
  • 00:08:54 that and with that let's simply create a
  • 00:08:56 new file here with ctrl + command N or
  • 00:08:58 by clicking that icon here and then
  • 00:09:01 simply and give it any name you want
  • 00:09:02 like first app dot J s dot J ass is of
  • 00:09:06 course important because we're creating
  • 00:09:07 a JavaScript app here now in there we
  • 00:09:11 can run any code nodejs is capable of
  • 00:09:14 executing and for this I will write a
  • 00:09:17 simple
  • 00:09:18 script I will simply write console.log
  • 00:09:21 hello from node J s now you could write
  • 00:09:25 this in the browser to the browser would
  • 00:09:27 be capable of running this and it would
  • 00:09:30 lock this and the JavaScript console in
  • 00:09:32 your browser dev tools now you can also
  • 00:09:34 execute this with no choice though for
  • 00:09:36 this go back to your terminal or command
  • 00:09:38 prompt and make sure you navigate into
  • 00:09:41 that new folder you created with the
  • 00:09:44 help of CD and so on you can
  • 00:09:47 alternatively also go back to visual
  • 00:09:49 studio code and there under view you can
  • 00:09:51 open that terminal and that will give
  • 00:09:54 you your default system terminal already
  • 00:09:57 navigated into that folder so that's a
  • 00:09:59 bit more convenient and then here you
  • 00:10:01 can run note and now first – fjs so that
  • 00:10:06 file name if you now it entered you
  • 00:10:09 should see hello from noches here
  • 00:10:11 because this execute your code and this
  • 00:10:14 already shows you that note is
  • 00:10:16 JavaScript on your computer this is not
  • 00:10:19 using a browser it's just using the
  • 00:10:21 internal engine the extended internal
  • 00:10:24 JavaScript engine used by the browser to
  • 00:10:27 execute your code locally totally
  • 00:10:29 outside of a browser and of course we
  • 00:10:32 can also do more than just logging
  • 00:10:34 something let's for example write this
  • 00:10:37 text to a file for this we can import
  • 00:10:41 something from the nodejs environment
  • 00:10:44 I mentioned that it would add new
  • 00:10:46 features to JavaScript right you have to
  • 00:10:48 import them and I'll come back to the
  • 00:10:50 detailed import syntax and which
  • 00:10:52 features exist a little bit later for
  • 00:10:55 now just follow along to have this first
  • 00:10:57 quick demo and create a new constant
  • 00:11:00 I'll also give you a brief refresher
  • 00:11:02 about constants and JavaScript in this
  • 00:11:04 course and then type require FS FS
  • 00:11:08 stands for file system and this syntax
  • 00:11:11 simply imports some file system
  • 00:11:14 functionalities into this javascript
  • 00:11:16 file so that we can use these
  • 00:11:18 functionalities there we can now use FS
  • 00:11:22 write file sync here to create a new
  • 00:11:26 file and this takes a couple of
  • 00:11:28 arguments and the first argument is the
  • 00:11:31 path where
  • 00:11:32 you want to store that foul and that
  • 00:11:34 also includes the filename now once you
  • 00:11:36 created in that same folder so I will
  • 00:11:38 simply name it hello dot txt for example
  • 00:11:41 and the second argument is today that we
  • 00:11:43 want to put in there and there I will
  • 00:11:45 just put hello from node.js with this if
  • 00:11:50 we now save this and we re execute this
  • 00:11:53 with node first fjs you should have a
  • 00:11:56 hello text file here and this gives you
  • 00:11:59 the text written in there and this is
  • 00:12:02 something you couldn't do in the browser
  • 00:12:04 because there you can't access your
  • 00:12:06 local file system and obviously its
  • 00:12:08 features like this which you need to
  • 00:12:10 then also create a server which we don't
  • 00:12:12 have at this point by the way this is
  • 00:12:14 not a website obviously it's just some
  • 00:12:16 utility script and this is something we
  • 00:12:19 can do with nodejs now this was a first
  • 00:12:21 quick demo we'll learn all the cool
  • 00:12:23 features and how this works and what
  • 00:12:26 exactly disks in Texas throughout the
  • 00:12:28 course of course
  • 00:12:33 so you hopefully got a first picture of
  • 00:12:36 what node.js is and why you use it and
  • 00:12:39 what you use it for now typically and
  • 00:12:41 also what we will do in this course you
  • 00:12:43 use node.js to run it on a server to
  • 00:12:46 write server-side code and for that we
  • 00:12:50 have to have a look at the full picture
  • 00:12:51 we have our users using client computer
  • 00:12:55 with a browser their mobile phone with a
  • 00:12:57 browser even mobile apps and we will
  • 00:13:00 come back to how nodejs can communicate
  • 00:13:03 with these later in the course too but
  • 00:13:05 for now let's stick to the traditional
  • 00:13:06 browser picture we get our users using
  • 00:13:09 their browser and there we can use HTML
  • 00:13:11 CSS and JavaScript javascript in the
  • 00:13:14 browser to create web pages right now
  • 00:13:18 they wizard a page my page comm and they
  • 00:13:21 send the request to do so for example by
  • 00:13:23 entering a URL in the browser a request
  • 00:13:25 is sent to that URL now there the server
  • 00:13:29 comes into play we got our server some
  • 00:13:32 computer running in the internet which
  • 00:13:33 has the IP associated with that domain
  • 00:13:36 which is automatically resolved for us
  • 00:13:38 and on that server we then want to
  • 00:13:41 execute some code that does something
  • 00:13:43 with the incoming requests and returns a
  • 00:13:46 response often but not necessarily
  • 00:13:49 always as you will learn this response
  • 00:13:51 is a HTML page which the browser that in
  • 00:13:54 turn can display by the way it is not
  • 00:13:57 necessarily just HTML it's all the
  • 00:13:59 things like CSS files or JavaScript
  • 00:14:01 files with JavaScript code for the
  • 00:14:03 browser not for the server now on the
  • 00:14:06 server we typically do tasks that we
  • 00:14:09 can't or don't want to do from inside
  • 00:14:11 the browser for performance or security
  • 00:14:13 reasons we connect to databases for
  • 00:14:16 example to fetch and store data we do
  • 00:14:19 user authentication which we obviously
  • 00:14:21 can only do on a place the user can't
  • 00:14:23 access to make it more secure and avoid
  • 00:14:26 it being hacked we do it for input
  • 00:14:29 validation to see if a user enter the
  • 00:14:31 correct email address the browser can
  • 00:14:33 always be tricked users can even edit
  • 00:14:36 their browser source ID code you can
  • 00:14:38 open the developer tools and start
  • 00:14:39 working on that page you're on but the
  • 00:14:42 server is of course sheltered from that
  • 00:14:44 the user can't access it
  • 00:14:46 and in general we have our business
  • 00:14:49 logic on the server
  • 00:14:50 everything our user shouldn't see which
  • 00:14:53 takes too much time to run in the
  • 00:14:54 browser where we obviously want to
  • 00:14:56 deliver a fast user experience or
  • 00:14:58 anything of that kind and that is where
  • 00:15:01 we use nodejs so all the JavaScript code
  • 00:15:04 but this time not on the browser but on
  • 00:15:06 the server and this is where we use the
  • 00:15:09 many features no chess gives us and this
  • 00:15:11 is how we indirectly allow our users to
  • 00:15:14 work with the server through that
  • 00:15:16 request response pattern the direct
  • 00:15:19 access is not available so this is how
  • 00:15:22 we will use no chess in this course also
  • 00:15:24 we will use it to write code on the
  • 00:15:27 server that returns data our users our
  • 00:15:30 clients can work with now one important
  • 00:15:33 side note at this point of time no js'
  • 00:15:37 is not limited to running code on a
  • 00:15:39 server it's a JavaScript runtime and you
  • 00:15:41 even saw a first demo which did not do
  • 00:15:44 anything where we needed a browser right
  • 00:15:46 we didn't spin up a server there we
  • 00:15:49 didn't do anything which we would have
  • 00:15:51 reached through a browser will do that a
  • 00:15:54 lot throughout the course but we haven't
  • 00:15:56 done it thus far because it's just a
  • 00:15:58 JavaScript runtime we can execute any
  • 00:16:00 JavaScript code with no chairs and often
  • 00:16:03 that is code that runs on a server and
  • 00:16:05 is executed upon incoming requests but
  • 00:16:08 you also often use node.js for average
  • 00:16:11 code for example for local utility
  • 00:16:14 scripts or Bell tools if you worked with
  • 00:16:16 let's say react or angular or view or
  • 00:16:19 anything of that kind you actually used
  • 00:16:21 note Reyes indirectly a lot for all the
  • 00:16:24 build processes these languages or
  • 00:16:27 frameworks needed because no task is a
  • 00:16:30 great tool for writing utility scripts
  • 00:16:33 you have access to the file system so
  • 00:16:35 you can write and read and manipulate
  • 00:16:37 files and this allows you to do a lot of
  • 00:16:39 utilities stuff on your computer that is
  • 00:16:41 never exposed to the public and I just
  • 00:16:44 want you to know that and I'll even have
  • 00:16:46 a section this course where I dive a
  • 00:16:48 little bit more in such build tools and
  • 00:16:50 non server-side language usages of
  • 00:16:53 node.js in general and that is the most
  • 00:16:57 popular thing you do with no chest
  • 00:16:59 though you
  • 00:17:00 in the context of web development and
  • 00:17:02 server-side code so you use it to run a
  • 00:17:05 server and actually and that is an
  • 00:17:06 important difference to PHP for example
  • 00:17:09 with nodejs
  • 00:17:10 you don't just write the code that is
  • 00:17:13 running on your server you also write
  • 00:17:16 the server yourself so the code that
  • 00:17:19 takes the incoming requests and routes
  • 00:17:22 them to your well our code in PHP you
  • 00:17:26 have extra tools like Apache or engines
  • 00:17:28 which run the servers which listen to
  • 00:17:31 incoming requests and then execute your
  • 00:17:33 PHP code here noches that's both it does
  • 00:17:37 that listening and it then also does
  • 00:17:40 whatever you wanna do in your code so
  • 00:17:42 that's important and that's something
  • 00:17:43 you'll see in action soon we also use it
  • 00:17:47 or we therefore also use it to run all
  • 00:17:49 our business logic so not just to listen
  • 00:17:52 to incoming requests but you then Rick
  • 00:17:55 with the requests data work with files
  • 00:17:57 work with databases all that fun stuff
  • 00:18:00 no trace is capable of and we'll do all
  • 00:18:03 that in this course obviously and we
  • 00:18:06 also handle the response site not just
  • 00:18:08 incoming requests you will also learn
  • 00:18:11 how you use no chairs to send back data
  • 00:18:13 to your clients be that HTML pages HTML
  • 00:18:18 pages with dynamic content or data only
  • 00:18:21 in the format of JSON or XML or even
  • 00:18:24 files so this is what we use no chance
  • 00:18:27 and what we will dive heavily into in
  • 00:18:29 this course alternatives to node.js
  • 00:18:32 would be things like Python also with
  • 00:18:36 frameworks like flask or Django or PHP
  • 00:18:39 with frameworks like level maybe or
  • 00:18:41 standalone vanilla PHP of course and
  • 00:18:43 more asp.net Ruby on Rails all that
  • 00:18:47 stuff this basically are all
  • 00:18:49 replacements for node.js or no trace can
  • 00:18:52 be a replacement for them and there is
  • 00:18:54 no clear winner all these languages are
  • 00:18:57 capable of doing the same kind of stuff
  • 00:18:59 and of course they differ in some
  • 00:19:01 technically regards but in general it's
  • 00:19:04 great to have that broad variety the
  • 00:19:07 huge advantage or one huge advantage of
  • 00:19:09 Noches is that it uses JavaScript a
  • 00:19:12 language which you need
  • 00:19:14 so much in modern web development for
  • 00:19:16 all the front-end for some built tools
  • 00:19:19 and if you den can also use it on the
  • 00:19:21 server side you don't need to learn a
  • 00:19:24 bunch of different languages you can use
  • 00:19:26 one at the same language and then use
  • 00:19:28 that for your server-side code too
  • 00:19:30 this is why no J's is a great language
  • 00:19:32 to learn you get so much efficiency out
  • 00:19:35 of it it's also a highly performant and
  • 00:19:37 popular language there are so many jobs
  • 00:19:39 out there for no chairs there never was
  • 00:19:42 a better time to learn it it's used in
  • 00:19:44 so many environments also for a lot of
  • 00:19:46 cutting edge stuff but in general no
  • 00:19:49 chairs is a great solution it's trending
  • 00:19:53 it's fast it's efficient and it makes
  • 00:19:56 sure that you only need to learn one
  • 00:19:57 language to write all the code you need
  • 00:20:00 in a modern web application
  • 00:20:06 now that we had a look at the basics
  • 00:20:08 about no chairs what we can do with it
  • 00:20:10 and now did we even use it let me walk
  • 00:20:13 you through the content of this course
  • 00:20:14 and explain what's in the course and
  • 00:20:17 where you can find what we're almost
  • 00:20:20 done getting started in the next module
  • 00:20:22 I will give you a brief JavaScript
  • 00:20:25 refresher just to make sure we're all on
  • 00:20:27 the same page regarding the core
  • 00:20:29 JavaScript syntax but more importantly
  • 00:20:31 about some next-gen JavaScript features
  • 00:20:35 which we'll use in a course basic
  • 00:20:37 JavaScript knowledge is assumed and will
  • 00:20:39 not be taught in that module but if you
  • 00:20:42 just need a refresher some additional
  • 00:20:44 information or this next-gen JavaScript
  • 00:20:47 knowledge then this is a module you
  • 00:20:48 should take otherwise it's optional now
  • 00:20:52 after this refresher or immediately
  • 00:20:54 after this module if you don't need a
  • 00:20:56 refresher we'll dive into the node.js
  • 00:20:58 basics how it generally works how we can
  • 00:21:01 work with streams of data and some core
  • 00:21:04 node.js modules so really the
  • 00:21:06 nitty-gritty details you need to know
  • 00:21:09 about the the language so to say and
  • 00:21:11 we'll then dive into efficiently
  • 00:21:14 developing with nodejs how you can debug
  • 00:21:18 your code handle errors and so on
  • 00:21:21 now thereafter we'll dive into expressjs
  • 00:21:24 the most popular framework you can use
  • 00:21:27 for no trace I'll explain why we use it
  • 00:21:30 and why we still in its heart use nodejs
  • 00:21:33 why we still learn about no chest even
  • 00:21:36 though we use Express J's because using
  • 00:21:39 it makes sense it makes things easier
  • 00:21:41 you will use it if you are working with
  • 00:21:43 no trace but still of course we still
  • 00:21:46 use nodejs
  • 00:21:47 with all of that now thereafter we'll
  • 00:21:50 dive into templating engines that are
  • 00:21:52 basically some tools you could say that
  • 00:21:54 allow you to generate HTML pages on the
  • 00:21:57 server with dynamic content encoded in
  • 00:22:00 them and that is how you can return
  • 00:22:02 different pages or pages with different
  • 00:22:04 content back to your users so that will
  • 00:22:07 be an important module now thereafter
  • 00:22:10 we'll dive into the
  • 00:22:11 model-view-controller pattern what that
  • 00:22:13 is and why we use it and of course how
  • 00:22:15 we use it and we'll then have a look at
  • 00:22:17 some advanced routing concepts
  • 00:22:19 outing is something we'll also learn in
  • 00:22:20 the expressjs module and we'll dive
  • 00:22:23 deeper into the models of the
  • 00:22:25 model-view-controller pattern now after
  • 00:22:28 we're done with that it's time to dive
  • 00:22:29 into databases and we'll start by
  • 00:22:32 connecting our node application with a
  • 00:22:34 sequel database my sequel to be precise
  • 00:22:37 as a next step we'll dive deeper into
  • 00:22:39 that and use a package that makes
  • 00:22:41 working with sequel databases easier
  • 00:22:43 sequel eyes now in after having seen
  • 00:22:46 sequel will continue with no sequel and
  • 00:22:50 see how we would use that and we'll use
  • 00:22:52 Mongoose there to also make working with
  • 00:22:54 that easier and we'll then stick to no
  • 00:22:57 sequel for the rest of the course but
  • 00:22:59 you'll fully learn both sequel and no
  • 00:23:02 sequel now after this we'll dive into
  • 00:23:04 sessions and cookies what that is why
  • 00:23:07 you use it and how you use it we'll have
  • 00:23:09 a look at authentication so how we can
  • 00:23:11 make sure that users can sign up and
  • 00:23:13 sign in with our application well dive
  • 00:23:16 into sending emails how that would work
  • 00:23:19 how can we auto generate emails when a
  • 00:23:22 user signs up for example we'll also
  • 00:23:25 type into advanced authentication
  • 00:23:27 features and also authorization which
  • 00:23:30 means that for example only a user who
  • 00:23:33 created a product can edit or delete it
  • 00:23:35 well then have a look at user input
  • 00:23:38 validation which is a crucial part in
  • 00:23:41 any web application because you want to
  • 00:23:43 make sure that the data your users enter
  • 00:23:45 into your forms is correct and valid and
  • 00:23:48 you'll learn how you can do that
  • 00:23:49 efficiently in this module and we'll
  • 00:23:52 then dive into error handling because
  • 00:23:54 errors will happen for example if the
  • 00:23:58 user enters wrong input and I will show
  • 00:24:00 you how you can handle different kinds
  • 00:24:02 of errors in an elegant way now
  • 00:24:05 thereafter we'll dive into something
  • 00:24:07 important file up and downloads
  • 00:24:10 how can you handle these how can you
  • 00:24:12 write code that takes incoming files
  • 00:24:15 stores them and is able to return files
  • 00:24:18 I'll also show you how you can
  • 00:24:20 automatically generate PDF documents on
  • 00:24:22 the server in this module well then have
  • 00:24:25 a look at pagination so how we can
  • 00:24:27 return our data in chunks instead of all
  • 00:24:30 the data at once which is not ideal for
  • 00:24:33 you
  • 00:24:33 our bandwidth will thereafter have a
  • 00:24:35 look at a sink request and I'll explain
  • 00:24:37 what it is and how we can handle it with
  • 00:24:39 no js' and we'll then dive into payments
  • 00:24:42 how can we accept payments through a
  • 00:24:45 service called stripe Jess obviously
  • 00:24:48 something we'll need in any shop we
  • 00:24:49 build and thereafter well dive into a
  • 00:24:53 different kind of application you can
  • 00:24:54 build with no chairs will dive into rest
  • 00:24:57 api's and when module does not sound
  • 00:25:00 like a lot of content on this very
  • 00:25:02 important technology but actually I got
  • 00:25:05 two modules on that and we'll also dive
  • 00:25:07 into file uploads and rest api's
  • 00:25:10 authentication pagination and all that
  • 00:25:12 stuff and additionally these modules
  • 00:25:15 will build up on the entire modules
  • 00:25:17 before that because you will learn the
  • 00:25:19 rest API does not differ that much from
  • 00:25:22 a normal node application and I will
  • 00:25:25 show you in detail which knowledge you
  • 00:25:27 still need and how you need to change
  • 00:25:29 some things not that many actually to
  • 00:25:32 turn an existing application into a REST
  • 00:25:35 API now and once we're done with that I
  • 00:25:38 will dive into more modern JavaScript
  • 00:25:42 feature async/await and how you can use
  • 00:25:44 that in your node application and we'll
  • 00:25:47 dive into WebSockets and socket IO this
  • 00:25:50 is a technology or a set of technologies
  • 00:25:52 that allows you to add real time
  • 00:25:55 functionalities to your app so for
  • 00:25:57 example if one user sends a message and
  • 00:26:00 you instantly want to notify another
  • 00:26:02 user you can use WebSockets for that and
  • 00:26:04 I will show how that works in this
  • 00:26:06 module after this module we'll dive into
  • 00:26:09 a different kind of api's we had a look
  • 00:26:12 at rest api's well then have a look at
  • 00:26:14 graph QL api's graph QL is a very modern
  • 00:26:17 and popular way of building api's it has
  • 00:26:21 some advantages over a REST API also
  • 00:26:24 some disadvantages and I will dive into
  • 00:26:26 graph QL and how it differs from REST
  • 00:26:28 API s in this module and I will show you
  • 00:26:31 how to build a graph QL API from scratch
  • 00:26:34 with no js' and this will be quite a big
  • 00:26:37 module even though it's only one block
  • 00:26:39 on this slide
  • 00:26:40 now thereafter we'll have a look at
  • 00:26:42 deployment how can you take your
  • 00:26:45 application you build all your No
  • 00:26:47 code you build how can you take that and
  • 00:26:49 move it to a real server in the web and
  • 00:26:51 ensure that it runs there this will be
  • 00:26:53 shown there
  • 00:26:54 there are also dive into things like
  • 00:26:56 setting up SSL encryption and once we're
  • 00:26:59 done with that I'll have a short module
  • 00:27:01 on node.js functionalities beyond
  • 00:27:04 creating web servers with it because you
  • 00:27:06 can use it for our things too as I will
  • 00:27:08 mention there and then we're done I'll
  • 00:27:11 end the course by giving you some next
  • 00:27:13 steps potential ideas on how to continue
  • 00:27:15 and by then you will have learned a lot
  • 00:27:19 about node.js and Express and the many
  • 00:27:23 other things we'll dive into in this
  • 00:27:24 course I hope you are as excited as I am
  • 00:27:28 and with that let's quickly move on so
  • 00:27:30 that we can quickly get to the point
  • 00:27:32 where we write code
  • 00:27:37 so now that you learned what's inside
  • 00:27:39 this course a whole lot as you can see
  • 00:27:41 let me explain to you how you can get
  • 00:27:44 the most out of this course because this
  • 00:27:46 is really important to me I want to make
  • 00:27:48 sure that you learn a lot now
  • 00:27:50 obviously you want to watch the videos
  • 00:27:52 now that seems pretty obvious but this
  • 00:27:55 is a video on the montt course watch the
  • 00:27:57 videos at your speed this is really
  • 00:27:59 important you can use the playback
  • 00:28:01 controls here and you Remy to fine tune
  • 00:28:03 the speed speed me up if I'm going too
  • 00:28:05 slow reduce the speed a bit if I'm going
  • 00:28:07 too fast
  • 00:28:08 also make sure to occasionally jump back
  • 00:28:12 to older sections or videos this is our
  • 00:28:15 normal process in learning sometimes you
  • 00:28:17 need to reiterate concepts you learned
  • 00:28:19 before you want to use that on the Mount
  • 00:28:22 nature of the course you don't have to
  • 00:28:24 go at the speed I'm going you don't have
  • 00:28:27 to go lecture by lecture and then never
  • 00:28:29 go back use the tools you got here use
  • 00:28:33 the course to its fullest potential and
  • 00:28:35 use the on-demand nature in this course
  • 00:28:38 we will also build projects and you will
  • 00:28:41 find a lot of exercises and assignments
  • 00:28:43 I encourage you to do these of course
  • 00:28:47 you can also just watch me code and just
  • 00:28:49 watch my solutions to the assignments
  • 00:28:51 but you will learn way more if you try
  • 00:28:53 to solve these things on your own and if
  • 00:28:55 you actively code along now one
  • 00:28:58 important note for this is of course
  • 00:29:00 that you want to do that also at your
  • 00:29:03 speed and by well of course all the
  • 00:29:06 falling along the videos but also make
  • 00:29:09 sure to Google for problems if you hit
  • 00:29:12 any not because I don't want to help you
  • 00:29:14 I will help you if you need help but
  • 00:29:17 because this makes you a better learner
  • 00:29:19 you need to be able to solve your
  • 00:29:21 problems on your own and I see too many
  • 00:29:23 students who have a ask first research
  • 00:29:27 later mentality and this is not really
  • 00:29:30 that great for you
  • 00:29:32 this makes me learn it better because I
  • 00:29:34 get challenged a lot which is great but
  • 00:29:36 you should learn more
  • 00:29:37 ultimately so definitely try to solve
  • 00:29:40 your problems on your own it will help
  • 00:29:42 you a lot
  • 00:29:44 although use all the course resources
  • 00:29:47 you find in there that's not just 2
  • 00:29:48 videos that's also the attached
  • 00:29:50 source code which you find attached to
  • 00:29:53 the last lecture of each module which
  • 00:29:55 you can use to compare to your code if
  • 00:29:57 you're facing any problems and again
  • 00:29:59 this is a great way of solving problems
  • 00:30:00 on your own
  • 00:30:02 by comparing the solutions and finding
  • 00:30:04 out what went wrong on your own so use
  • 00:30:06 all these resources also make sure to
  • 00:30:10 use the additional links I occasionally
  • 00:30:12 post in some lectures where you can
  • 00:30:14 research a bit more about certain topics
  • 00:30:16 and dive a bit deeper in cases where
  • 00:30:19 you're interested in that topic now last
  • 00:30:22 but not least this course like all udemy
  • 00:30:25 courses has a Q&A section you can post
  • 00:30:27 questions on every video there's a Q&A
  • 00:30:30 button at the bottom of the video or in
  • 00:30:32 the course dashboard the general Q&A
  • 00:30:34 section and you can search there make
  • 00:30:37 sure to also search the Q&A before you
  • 00:30:39 post often a question has already been
  • 00:30:42 answered but you can also post there if
  • 00:30:44 you got a question now the thing about
  • 00:30:47 asking is asking is easy answering is
  • 00:30:50 hard so try helping others to take this
  • 00:30:54 opportunity of answering to questions of
  • 00:30:57 other students simply because you again
  • 00:31:00 get challenged and if you're wrong that
  • 00:31:03 doesn't matter
  • 00:31:04 you'll be corrected and you'll learn
  • 00:31:05 again but you will learn so much by
  • 00:31:08 being forced to think about a problem
  • 00:31:10 articulate a solution and the best of
  • 00:31:13 all you also help other students so help
  • 00:31:15 each other
  • 00:31:16 I will also help you of course but if
  • 00:31:18 you do all these things you'll get a lot
  • 00:31:20 out of this course promised but with
  • 00:31:23 dead let's leave this first module and
  • 00:31:25 continue with the course
  • 00:31:31 now before we finally dive into the
  • 00:31:33 JavaScript refresher or optionally
  • 00:31:36 incase you skipped that into the node.js
  • 00:31:38 basics let me dive into two different
  • 00:31:41 ways of executing your node code one way
  • 00:31:45 of executing it which I briefly
  • 00:31:46 mentioned earlier in this module is the
  • 00:31:49 repple rappelled stands for read reading
  • 00:31:52 user input evil evaluating user input
  • 00:31:56 print outputting a result and l loop for
  • 00:32:01 returning and waiting for a new input
  • 00:32:03 and the repple is what you use if you
  • 00:32:06 just type node into your terminal Here I
  • 00:32:10 am in the terminal or on Windows this
  • 00:32:12 would be your command prompt and there
  • 00:32:13 if you just type node with node
  • 00:32:15 installed you are now in the rebel which
  • 00:32:17 you can identify by the fact that you
  • 00:32:19 now don't have that full path and your
  • 00:32:22 computer name at the beginning but just
  • 00:32:23 that greater than sign and there you can
  • 00:32:27 execute node commands like console.log
  • 00:32:30 2+2 or also writing interacting with
  • 00:32:34 files you can import the file system
  • 00:32:37 there just as we did it previously in
  • 00:32:39 our file and then we can use that to
  • 00:32:42 write a file synchronously and so on so
  • 00:32:45 that will all work here and this is of
  • 00:32:48 course an environment where we don't
  • 00:32:49 store our code and files but you write
  • 00:32:52 our node application with every line the
  • 00:32:55 lines don't work independent from each
  • 00:32:57 other so I can now use the file system
  • 00:33:00 package for example now that I imported
  • 00:33:02 it one line earlier but once I quit out
  • 00:33:05 of this process with control C I'm done
  • 00:33:09 this is not saved anywhere this is not a
  • 00:33:11 file I can execute again and this
  • 00:33:13 therefore is a great playground the
  • 00:33:15 alternative to running codes is that you
  • 00:33:17 execute files as we did it before and as
  • 00:33:20 we we will basically do it for the rest
  • 00:33:22 of this course this is the alternative
  • 00:33:23 to using the rebel when you execute
  • 00:33:26 files the advantage of course is and
  • 00:33:28 that is why we use it for real apps that
  • 00:33:30 we write our code in advance and we can
  • 00:33:32 always execute it and when we build a
  • 00:33:35 real app we want to save our code of
  • 00:33:36 course in files which we can then deploy
  • 00:33:39 in the end which we could share with our
  • 00:33:41 developers and where we can also pause
  • 00:33:43 our work
  • 00:33:44 pick up later and we're we never lose
  • 00:33:46 the code we write but the rattle is a
  • 00:33:49 great playground because we can use that
  • 00:33:51 to run some commands to try out certain
  • 00:33:53 things because we execute code as we
  • 00:33:55 write it and therefore these are the two
  • 00:33:57 ways of running your node code obviously
  • 00:34:01 we'll go with the store code and files
  • 00:34:03 approach in this course but whenever I
  • 00:34:05 say rebel I'm referring to this direct
  • 00:34:08 input way which I can recommend for
  • 00:34:11 trying out some features but not for
  • 00:34:13 writing real applications that is not
  • 00:34:16 what you would do I just wanted to get
  • 00:34:18 these two ways out of the way we'll
  • 00:34:20 focus on the left one here and with that
  • 00:34:22 let's dive into writing some code with
  • 00:34:25 the next modules
  • 00:34:35 so let's style and what better way to
  • 00:34:40 start with it then of course from
  • 00:34:42 scratch with its basics in this module
  • 00:34:45 you will learn how the web actually
  • 00:34:48 works this will only be a brief
  • 00:34:50 refresher but since we will of course
  • 00:34:52 use nodejs in this course to build a
  • 00:34:55 server a web application and write
  • 00:34:57 server-side code we obviously have to
  • 00:35:00 make sure that we all understand how the
  • 00:35:02 web works and which role nodejs plays in
  • 00:35:06 that bigger picture now this will only
  • 00:35:09 be a brief refresher obviously we'll
  • 00:35:11 then dive into creating such a node.js
  • 00:35:13 server and running node.js code when
  • 00:35:17 requests are reaching that server and of
  • 00:35:20 course we'll also send back responses
  • 00:35:22 for this will also use node core modules
  • 00:35:26 like the filesystem module you saw in
  • 00:35:28 the first course section we'll use
  • 00:35:31 another very important core module in
  • 00:35:33 this section which is related to
  • 00:35:36 creating such a web server now as I
  • 00:35:38 already mentioned an important part of
  • 00:35:40 creating a web server is of course that
  • 00:35:43 we're able to handle requests and
  • 00:35:45 responses so that you learn how you can
  • 00:35:48 use nodejs and the many features it
  • 00:35:50 offers to accept incoming requests and
  • 00:35:53 do something with them
  • 00:35:55 for example parse them and how to send
  • 00:35:58 back responses these are crucial things
  • 00:36:01 you got to know now related to all of
  • 00:36:04 that will learn how nodejs uses
  • 00:36:07 asynchronous code and a concept called
  • 00:36:09 the event loop to stay reactive and
  • 00:36:13 ensure that our script never slows down
  • 00:36:16 and always works just fine so with that
  • 00:36:20 let's dive into node.js
  • 00:36:26 so let me quickly refresh our knowledge
  • 00:36:29 on how the web works if you're totally
  • 00:36:32 aware of all of this you can of course
  • 00:36:33 skip this lecture now the web works like
  • 00:36:36 this
  • 00:36:36 we got a user a client maybe you sitting
  • 00:36:40 in front of your browser visiting a web
  • 00:36:42 page we're already being on a web page
  • 00:36:44 and submitting a form so you're
  • 00:36:46 interacting with web pages let's say you
  • 00:36:49 were visiting it so you're entering some
  • 00:36:51 URL into your browser and what happens
  • 00:36:55 behind the scenes is actually that the
  • 00:36:56 browser reaches out to some domain name
  • 00:37:00 servers to look that domain up because
  • 00:37:02 this domain is not really the address of
  • 00:37:05 your server
  • 00:37:06 it's basically an encoded human readable
  • 00:37:09 version of that address you could say
  • 00:37:11 you're a server itself has just an IP
  • 00:37:14 address but this is just some technical
  • 00:37:16 thing behind the scenes in the end you
  • 00:37:18 enter this URL and it will lead to some
  • 00:37:21 server you therefore or the browser
  • 00:37:23 therefore sends a request to that server
  • 00:37:27 with that given IP address I mentioned
  • 00:37:30 so the IP address belonging to that
  • 00:37:32 domain now that's far that's all
  • 00:37:34 interesting but now we reach the part
  • 00:37:36 where nodejs comes into play where your
  • 00:37:39 Noche s code matters you write the code
  • 00:37:43 that runs on that computer in the
  • 00:37:45 internet which has that IP address you
  • 00:37:48 write the code that spins up that server
  • 00:37:51 which is able to handle the incoming
  • 00:37:53 request and do something with it now you
  • 00:37:55 don't need to use node.js for this you
  • 00:37:58 could use PHP as p.net Ruby on Rails
  • 00:38:01 anything like that but in this course
  • 00:38:03 will obviously use nodejs
  • 00:38:05 because well it's a no trace course now
  • 00:38:09 in this code you can do all kinds of
  • 00:38:11 things and I already mentioned this in
  • 00:38:13 the first course module user input
  • 00:38:15 validation communicating with the
  • 00:38:17 database maybe which runs on a separate
  • 00:38:19 database server but which you typically
  • 00:38:21 reach out you from your back-end so your
  • 00:38:24 server-side code and once you're done
  • 00:38:26 with that you do one important thing you
  • 00:38:29 send back a response to the client this
  • 00:38:33 response can be some HTML text some HTML
  • 00:38:36 code which is then handled by the client
  • 00:38:39 but it could also be some other kind of
  • 00:38:41 data like a file some JSON or XML data
  • 00:38:45 the response is more than just a content
  • 00:38:48 by the way a response and also request
  • 00:38:51 also has headers this is some meta
  • 00:38:54 information which is attached to request
  • 00:38:56 and response describing what's inside it
  • 00:38:58 for example but will see this too so
  • 00:39:00 this is how the web generally works and
  • 00:39:03 nodejs is the part we will focus on it
  • 00:39:06 is the code that makes up that server in
  • 00:39:09 the end now that request and response
  • 00:39:11 transmission is done through some
  • 00:39:14 protocol so basically a standardized way
  • 00:39:16 of communicating you could say because
  • 00:39:18 obviously to correctly handle a request
  • 00:39:22 and send back a response the browser can
  • 00:39:25 work with we have to follow some rules
  • 00:39:27 and these rules are defined by the
  • 00:39:29 protocol we use HTTP or HTTPS HTTP
  • 00:39:34 stands for hypertext Transfer Protocol
  • 00:39:37 and there we simply define what it is
  • 00:39:39 defined how a valid request looks like
  • 00:39:42 and how the data should be transferred
  • 00:39:44 from browser to server and the other way
  • 00:39:46 around
  • 00:39:47 an HTTP simply is the same with SSL
  • 00:39:50 encryption turned on where all the data
  • 00:39:53 that is transmitted is actually
  • 00:39:55 encrypted so that if anyone is spoofing
  • 00:39:58 your connection they can't read your
  • 00:40:01 data now towards the end of the course I
  • 00:40:04 will show you how to enable HTTPS for
  • 00:40:06 the majority will just use HTTP since
  • 00:40:09 will only be developing that code will
  • 00:40:12 only work on it locally but once you put
  • 00:40:14 it into production I will also show you
  • 00:40:17 how to turn on that SSL encryption this
  • 00:40:21 is how the web works in a nutshell and
  • 00:40:24 how nodejs is related to it and this is
  • 00:40:26 exactly where we will now continue
  • 00:40:28 working with no chairs and where we will
  • 00:40:30 now finally create a server with no
  • 00:40:33 chairs
  • 00:40:37 I'm back in Visual Studio code D IDE
  • 00:40:40 I'll use throughout this course and this
  • 00:40:42 is again a totally empty folder I only
  • 00:40:44 got my get ignore file in there because
  • 00:40:46 I will use version management here get
  • 00:40:48 you don't need to use that at all if it
  • 00:40:50 doesn't tell you anything you can just
  • 00:40:52 ignore that you don't need that git
  • 00:40:54 ignore file so I have an empty folder
  • 00:40:56 and again I will now create a new file
  • 00:40:59 with command and or by pressing this
  • 00:41:01 icon here and this file can have any
  • 00:41:04 name you want but often you name it
  • 00:41:06 server J s or app dot J s because it is
  • 00:41:11 the root file that makes up your node.js
  • 00:41:14 application so the nodejs code you will
  • 00:41:17 execute on a computer in the cloud on a
  • 00:41:19 server in the end so I'll name it app J
  • 00:41:22 s here now in this file I want to create
  • 00:41:25 a server fruit ojs that's far in the
  • 00:41:28 first module we only saw how we can work
  • 00:41:31 with the file system so how can we now
  • 00:41:34 spin out such a server we again need to
  • 00:41:37 import some functionality because the
  • 00:41:40 way JavaScript works both in the browser
  • 00:41:43 and nodejs
  • 00:41:44 there is a handful of functions and
  • 00:41:48 objects we can use globally without
  • 00:41:50 importing anything into that file but
  • 00:41:53 generally most functionalities are now
  • 00:41:55 available by default to not pollute our
  • 00:41:58 global name space with all these
  • 00:42:00 reserved key words and names basically
  • 00:42:02 and also to make it very obvious in each
  • 00:42:04 file on which functionalities this file
  • 00:42:07 depends thus far this file does not
  • 00:42:09 depend on anything now there are a
  • 00:42:12 couple of core modules no treasure ships
  • 00:42:15 with and as you will learn from the
  • 00:42:16 course you can also install third-party
  • 00:42:18 modules which do not ship with node but
  • 00:42:21 let's stick to the core modules for now
  • 00:42:22 here are a handful of them now not all
  • 00:42:25 core modules just some and as you can
  • 00:42:28 see there is FS which we already used
  • 00:42:30 thus far there also is path which helps
  • 00:42:33 us with constructing puffs so paths to
  • 00:42:36 files on a file system that work on any
  • 00:42:40 operating system because Windows and Mac
  • 00:42:43 and Linux use different path formats
  • 00:42:45 there is the OS package which helps us
  • 00:42:48 with operating system relevant
  • 00:42:50 information
  • 00:42:51 and so on and there are the two top most
  • 00:42:53 packages HTTP and HTTPS and as you might
  • 00:42:57 be able to guess these to sound very
  • 00:43:00 helpful when it comes to creating a
  • 00:43:02 server and working with HTTP requests
  • 00:43:04 and HTTP responses and indeed HTTP helps
  • 00:43:09 us with launching a server or also with
  • 00:43:11 our tests like sending requests because
  • 00:43:14 a node app could also send a request to
  • 00:43:16 another server you can have multiple
  • 00:43:19 servers communicate with each other for
  • 00:43:21 example you could send a request to the
  • 00:43:23 Google Maps API to send some coordinates
  • 00:43:26 and get back an address but that's just
  • 00:43:29 an example let's keep it simple here and
  • 00:43:31 let's focus on the launch a server
  • 00:43:33 aspect HTTP would be helpful when we
  • 00:43:36 want to launch an SSL encoded server so
  • 00:43:39 where all the data which is transferred
  • 00:43:40 is encrypted and as I mentioned earlier
  • 00:43:42 this is something I'll come back to
  • 00:43:44 towards the end of the course now with
  • 00:43:47 that let's use that HTTP module and to
  • 00:43:51 use it we need to import it so we
  • 00:43:54 basically need to make sure that we can
  • 00:43:56 use features from that HTTP module which
  • 00:44:00 nodejs ships with but which still is not
  • 00:44:03 available globally by default we need to
  • 00:44:06 make sure that we can use these features
  • 00:44:08 in this file and for this we import that
  • 00:44:12 functionality we do this by creating a
  • 00:44:15 new constant and you could create a war
  • 00:44:18 or use let to but since we'll have some
  • 00:44:22 value here which we will never change we
  • 00:44:25 can also just use a cons to make this
  • 00:44:27 really clear that we will never touch
  • 00:44:29 this again we'll use it but will not
  • 00:44:31 overwrite it and you can give this any
  • 00:44:33 name you want
  • 00:44:34 but typically you keep the name of the
  • 00:44:37 module you're importing so I'll name
  • 00:44:39 this HTTP but again you could rename
  • 00:44:41 this to whatever you want then you have
  • 00:44:44 an equal sign and now we need to assign
  • 00:44:46 a value and now there's a special
  • 00:44:48 keyword a special function Noches does
  • 00:44:52 expose globally so you can use it by
  • 00:44:54 default in any file you run by a note
  • 00:44:57 chess and that is the require a keyword
  • 00:45:00 now this is simply the way you import
  • 00:45:03 files in note
  • 00:45:05 yes require ever takes a path to another
  • 00:45:09 file you can also import your own
  • 00:45:11 JavaScript files but will not do this
  • 00:45:14 for now we'll do this heavily throughout
  • 00:45:16 the course though or if you don't have a
  • 00:45:18 path to one of your files you can also
  • 00:45:21 import a core module like HTTP by the
  • 00:45:25 way a path to one of your files always
  • 00:45:28 has to start with dot slash or slash if
  • 00:45:32 it's an absolute path dot slash would be
  • 00:45:34 a relative path so this would lead to
  • 00:45:36 the same folder and would now look for a
  • 00:45:38 HTTP file by the way it automatically
  • 00:45:41 adds dot J s at the end you don't need
  • 00:45:44 to add it on your own but you can but
  • 00:45:46 this would now look for a local file
  • 00:45:48 named HTTP if you omit dot slash or
  • 00:45:51 slash at the beginning it will not look
  • 00:45:54 for a local file so even if you had a
  • 00:45:56 file named HD PJs it would not import
  • 00:46:00 this file let's get rid of it but
  • 00:46:02 instead it will always look for a global
  • 00:46:04 module named HTTP and indeed such a
  • 00:46:08 module exists because Noches ships with
  • 00:46:10 it so now we got this imported and now
  • 00:46:14 we can start using functionalities from
  • 00:46:16 that global module and you can see that
  • 00:46:18 if you type HTTP dot this is how you
  • 00:46:22 access functions or so-called methods
  • 00:46:25 and properties on objects in JavaScript
  • 00:46:28 and as you can see this HTTP object
  • 00:46:31 which we import from the HTTP module has
  • 00:46:33 a bunch of fields and methods we can
  • 00:46:36 execute most importantly it has the
  • 00:46:39 create server method now as the name
  • 00:46:42 suggests this is a crucial method when
  • 00:46:45 it comes to well creating a server and
  • 00:46:48 create server actually if we hover over
  • 00:46:51 it we can see it actually takes a so
  • 00:46:54 called request listener as an argument a
  • 00:46:57 request listener simply is a function
  • 00:47:00 that will execute for every incoming
  • 00:47:03 request so let's define such a function
  • 00:47:06 for this I'll create a new function with
  • 00:47:09 the function keyword we can name it
  • 00:47:12 however you want our queue listener or
  • 00:47:14 whatever you want the name is totally up
  • 00:47:16 to you and this function
  • 00:47:18 has to receive two arguments you can see
  • 00:47:21 that here if you hover over that the
  • 00:47:23 request listener receives a request
  • 00:47:26 which is of type incoming message and a
  • 00:47:29 response object so in short Noches
  • 00:47:32 automatically gives us some object that
  • 00:47:35 represents the incoming request and
  • 00:47:37 allows us to read data from that request
  • 00:47:39 and it gives us an object response which
  • 00:47:43 we can use to return a response to
  • 00:47:45 whoever sent that request so now we have
  • 00:47:48 to accept these two arguments here and
  • 00:47:50 you can again name the arguments however
  • 00:47:52 you want you just have to keep in mind
  • 00:47:54 that the first one will contain data
  • 00:47:56 about two requests and the second one
  • 00:47:58 will help you send a response so I'll
  • 00:48:01 name it req andres and these are typical
  • 00:48:03 shortcuts you often see now this is a
  • 00:48:06 function our cue listener and now we
  • 00:48:08 can't pass this function reference so we
  • 00:48:11 don't execute it don't set these curly
  • 00:48:14 braces just pass the name to that
  • 00:48:17 function because this will simply tell
  • 00:48:19 create server Hey please look for this
  • 00:48:22 function with this name and executed for
  • 00:48:24 every incoming request so this is now
  • 00:48:27 when we set up this function will now
  • 00:48:29 run for every request that reaches our
  • 00:48:32 server which will be started by calling
  • 00:48:35 create server or almost one pieces
  • 00:48:38 missing I'll come back to that now this
  • 00:48:41 is one way of doing it now you don't
  • 00:48:44 have to explicitly create such a
  • 00:48:45 function though you can also use a so
  • 00:48:48 called anonymous function so here you
  • 00:48:51 can also type function req rest like
  • 00:48:54 this this is now a function without a
  • 00:48:56 name that is why it's called anonymous
  • 00:48:58 and it still achieves the same we passed
  • 00:49:00 that function to create server and
  • 00:49:02 therefore node will execute this
  • 00:49:05 function whenever a request reaches our
  • 00:49:08 server this is a event-driven
  • 00:49:11 architecture nodejs uses heavily you
  • 00:49:14 work a lot with such setups or such code
  • 00:49:17 snippets where you tell node if X
  • 00:49:20 happens do Y so in this case if a
  • 00:49:24 request comes please execute this
  • 00:49:26 function now you can also use next-gen
  • 00:49:30 JavaScript syntax and you
  • 00:49:32 Eero function where you omit the
  • 00:49:34 function keyword and just have the two
  • 00:49:36 arguments followed by an equal sign and
  • 00:49:39 a greater sign hence an arrow and then
  • 00:49:42 the function body this is basically the
  • 00:49:45 equivalent to the function keyword
  • 00:49:47 approach okay so this is our create
  • 00:49:50 server callback function as it's called
  • 00:49:53 it's called by no chairs whenever a
  • 00:49:56 request reaches our server for now let's
  • 00:49:59 simply console.log the request object to
  • 00:50:02 see what's inside now if we execute this
  • 00:50:05 file we can do this in the built-in
  • 00:50:07 terminal which is already navigated into
  • 00:50:09 this project folder make sure you go
  • 00:50:12 into that project folder if you're using
  • 00:50:14 the terminal outside of that IDE so once
  • 00:50:17 you are in a terminal navigated in the
  • 00:50:19 folder you can run note and then app J s
  • 00:50:22 this will execute the App J's file and
  • 00:50:25 let's see what it does
  • 00:50:27 hmm nothing right we don't see
  • 00:50:29 console.log and that makes sense because
  • 00:50:32 we didn't send a request to the server
  • 00:50:34 but we don't even know where the server
  • 00:50:36 is how do we reach that server which
  • 00:50:39 address does it have well one important
  • 00:50:42 thing is missing this creates server
  • 00:50:45 method actually returns a server so we
  • 00:50:49 have to store that in a new variable or
  • 00:50:51 constant and I'll use a constant because
  • 00:50:53 I'll never overwrite it
  • 00:50:55 I only create a server once so now what
  • 00:50:57 I created server is stored here and now
  • 00:51:00 we can use that server and do something
  • 00:51:03 with it as you can see we get a bunch of
  • 00:51:05 methods we can call and one important
  • 00:51:07 method is listen-listen now actually
  • 00:51:10 starts a process where nodejs
  • 00:51:13 will not immediately exit our script but
  • 00:51:17 where no J's will instead keep this
  • 00:51:19 running to listen that's why the method
  • 00:51:22 is named like this for incoming requests
  • 00:51:25 now listen as you can see it takes a
  • 00:51:28 couple of arguments optional arguments
  • 00:51:31 the first one is the port on which you
  • 00:51:33 want to listen now in production you
  • 00:51:35 typically would not fill this out and it
  • 00:51:38 would take default of port 80 but here
  • 00:51:41 on local development we want to use a
  • 00:51:43 different port and you can also define
  • 00:51:45 hostname now by default this will be the
  • 00:51:48 name of the machine this is running on
  • 00:51:50 so for our local machine this is
  • 00:51:52 localhost by default so let's just pass
  • 00:51:55 a port 3000 is a port you often use but
  • 00:51:58 you're relatively free to use any port
  • 00:52:01 you want that thousands port are
  • 00:52:03 typically pretty safe and now with that
  • 00:52:06 if we re execute this you see one
  • 00:52:10 important thing the cursor here in the
  • 00:52:13 terminal doesn't go back in a new line
  • 00:52:15 because this process here is now still
  • 00:52:19 running it didn't finish this file
  • 00:52:22 execution didn't finish because we now
  • 00:52:25 get an ongoing looping process where
  • 00:52:28 this will keep on listening for requests
  • 00:52:31 and this is obviously what you want
  • 00:52:32 right you want to have a web server that
  • 00:52:34 keeps on listening for requests now we
  • 00:52:38 can see that in action by opening a new
  • 00:52:40 browser window where we simply enter
  • 00:52:43 localhost 3000 once you did that nothing
  • 00:52:48 happens because we haven't configured to
  • 00:52:50 return any HTML page but if you go back
  • 00:52:53 to your terminal you see a lot of output
  • 00:52:56 there and that is this line this is your
  • 00:53:01 request being locked to the console now
  • 00:53:04 let's analyze what happened here in
  • 00:53:06 detail and what's inside this request
  • 00:53:09 over the next lectures but these view
  • 00:53:12 lines here already give you a fully
  • 00:53:15 functional or almost fully functional
  • 00:53:17 web server and this is how you create
  • 00:53:21 servers in nodejs
  • 00:53:23 and I know that this can be hard to wrap
  • 00:53:25 your head around because it was for me
  • 00:53:28 when I started out with no js' years ago
  • 00:53:30 it was difficult to understand that
  • 00:53:33 coming from a PHP background you
  • 00:53:36 suddenly write your own server that
  • 00:53:38 sounded like something super complex
  • 00:53:40 well actually it's just these few lines
  • 00:53:43 and from now on we just will have to
  • 00:53:45 focus on doing something meaningful with
  • 00:53:47 the request and important sending back a
  • 00:53:51 response so time for detailed analysis
  • 00:53:54 in the next lectures
  • 00:53:59 in the last lecture we wrote our first
  • 00:54:02 little note chess server now this server
  • 00:54:05 is not doing all it shipped you it's not
  • 00:54:07 returning a response for example but
  • 00:54:10 it's generally showing us how we do
  • 00:54:12 create through a site code with no chess
  • 00:54:15 and how we do create our own servers now
  • 00:54:18 let's understand what happened there we
  • 00:54:20 executed that file with note app chess
  • 00:54:22 because our file was named app jeaious
  • 00:54:25 and this essentially started the script
  • 00:54:27 where nodejs went fruity and her file
  • 00:54:30 parsed the code registered the variables
  • 00:54:33 and functions and so on so it basically
  • 00:54:35 read our entire code and started
  • 00:54:37 executing it but then something
  • 00:54:39 important happened we never left that
  • 00:54:42 program right the reason for this is an
  • 00:54:45 important concept in know chess called
  • 00:54:47 the event loop this is basically a loop
  • 00:54:50 process which is managed by no trace
  • 00:54:53 which keeps on running as long as there
  • 00:54:56 is work to do you could say it keeps on
  • 00:54:59 running as long as there are event
  • 00:55:01 listeners registered and one event
  • 00:55:03 listener we did register and we never
  • 00:55:05 unregistered is that incoming request
  • 00:55:09 listener we passed or we set up with the
  • 00:55:12 help of create server we passed a
  • 00:55:14 function to create server and that is
  • 00:55:16 basically an ongoing event listener when
  • 00:55:18 we didn't unregister from and we
  • 00:55:21 shouldn't because our server should of
  • 00:55:23 course stay up and running so our core
  • 00:55:26 node application basically is managed by
  • 00:55:29 this event loop all our code is managed
  • 00:55:32 by this and as I mentioned note Reyes
  • 00:55:34 uses such an event-driven approach for
  • 00:55:37 all kinds of stuff not just for managing
  • 00:55:39 that server but that is of course a
  • 00:55:41 crucial part but you will see it a lot
  • 00:55:43 throughout this course for example when
  • 00:55:45 we later access a database you will see
  • 00:55:47 that there we also basically send that
  • 00:55:50 please insert some data request and then
  • 00:55:53 we register some function that should be
  • 00:55:55 executed once it's done and nodejs uses
  • 00:55:59 this pattern because they actually
  • 00:56:00 execute single threaded JavaScript so
  • 00:56:04 the entire node process basically uses
  • 00:56:07 one fret on our computer it's running on
  • 00:56:10 now as you might guess if
  • 00:56:13 create a server with noches it should of
  • 00:56:15 course be able to handle multiple
  • 00:56:18 thousands tens of thousands or hundreds
  • 00:56:20 of thousands of incoming requests and if
  • 00:56:24 it would always pause and then do
  • 00:56:26 something with the request this would
  • 00:56:28 not be that great
  • 00:56:29 hence it uses this event loop concept
  • 00:56:31 where in the end it always keeps on
  • 00:56:33 running and just executes code when a
  • 00:56:37 certain event occurs so that in general
  • 00:56:39 it's always available and whilst this
  • 00:56:42 might still sound like okay but if I got
  • 00:56:44 two incoming requests it needs to handle
  • 00:56:46 queue events well it is super fast in
  • 00:56:49 handling these requests and actually
  • 00:56:52 behind the scenes it does some
  • 00:56:54 multi-threading by leveraging the
  • 00:56:56 operating system but this event loop is
  • 00:56:59 a core thing you have to keep in mind
  • 00:57:01 that no js' basically has an ongoing
  • 00:57:03 loop as long as there are listeners and
  • 00:57:06 create server creates a listener which
  • 00:57:08 never stops but if you eventually were
  • 00:57:11 to unregister and you can do this with
  • 00:57:14 process not exit it would end and we can
  • 00:57:17 see that too if we go back to our code
  • 00:57:19 and after this line we tie a process dot
  • 00:57:24 exit and execute this function and we
  • 00:57:27 clear our console and we now repeat no
  • 00:57:30 tap J s you see it's now still running
  • 00:57:33 because this function never executed we
  • 00:57:36 had no incoming request yet but if we
  • 00:57:39 now reload this page localhost 3000 you
  • 00:57:44 see we still lock that request but then
  • 00:57:47 we're back in a new line in the terminal
  • 00:57:49 because now it did quit that process it
  • 00:57:52 didn't before but now it did it did quit
  • 00:57:55 that process with the help of process
  • 00:57:58 exit now typically you don't call that
  • 00:58:01 in your code because you don't want to
  • 00:58:03 quit your server if it quits people will
  • 00:58:06 not be able to reach your web page
  • 00:58:07 anymore but this is important for
  • 00:58:09 understanding how this works process
  • 00:58:12 exit basically Hart exited our event
  • 00:58:16 loop and therefore the program shuts
  • 00:58:18 down because there was no more work to
  • 00:58:20 do no TRAI saw that there is no more
  • 00:58:22 work to do and it basically closed the
  • 00:58:25 program and gave control
  • 00:58:27 to our terminal here so this is how we
  • 00:58:30 execute such an odious file and how we
  • 00:58:33 generally create such a server and what
  • 00:58:36 noches does behind the scenes let's see
  • 00:58:39 how we can now also work with the
  • 00:58:42 requests and with the response object we
  • 00:58:45 have in debt anonymous function here
  • 00:58:52 so let's go back to the request object
  • 00:58:55 relocked here now important just to keep
  • 00:58:58 that in mind this request object is the
  • 00:59:01 object noches generated for us with all
  • 00:59:04 the data off the incoming request when
  • 00:59:07 we visited localhost 3000 which we in
  • 00:59:10 turn can do because we listen to
  • 00:59:12 requests on that port so this is the
  • 00:59:15 request object if we have a look at it
  • 00:59:18 we see it's a very complex object
  • 00:59:21 there's lots of data in it it's not just
  • 00:59:23 data these are also partly functions we
  • 00:59:26 can call and so on
  • 00:59:27 so this is quite a complex object but we
  • 00:59:30 also see that for example we have some
  • 00:59:33 headers here headers as I mentioned
  • 00:59:35 earlier our metadata meta information
  • 00:59:37 added to a request and also to responses
  • 00:59:41 by the way and there we see for example
  • 00:59:43 the host this was sent to the request
  • 00:59:46 was sent to you we saw some headers
  • 00:59:49 attached by the browser like for example
  • 00:59:51 how the response data should be cached
  • 00:59:54 and stuff like that which browser we
  • 00:59:56 used for that request which kind of
  • 00:59:59 response we would accept that we accept
  • 01:00:00 some HTML XML and so on that we would
  • 01:00:05 accept encoded responses so where the
  • 01:00:07 response is actually minified to well
  • 01:00:10 safe bandwidth and so on
  • 01:00:12 sure all's it was a cookie attached even
  • 01:00:14 we have learned about cookies yet we
  • 01:00:16 will sell later but this was attached at
  • 01:00:18 some Google Analytics cookie we saw
  • 01:00:21 which HTTP version was used
  • 01:00:25 and so on so there's a lot we can gain
  • 01:00:28 from that request but a bit too much now
  • 01:00:32 there are only a few important fields
  • 01:00:35 you typically need the first important
  • 01:00:38 or interesting field is the URL
  • 01:00:42 now let's output dad and let's all the
  • 01:00:45 output you can output more than one
  • 01:00:47 value by separating them with commas
  • 01:00:49 request method and all the request
  • 01:00:52 headers here
  • 01:00:54 let's output these free values and
  • 01:00:57 restart the server with node app.js so
  • 01:01:00 now it's again listening and let's
  • 01:01:02 reload that page on localhost 3000 if we
  • 01:01:05 do so now we see the output has changed
  • 01:01:08 we still have all the header stuff
  • 01:01:10 because we're outputting request headers
  • 01:01:12 but prior to that we output the method
  • 01:01:15 which you see here it's get and your CD
  • 01:01:18 URL and the URL is just a slash here
  • 01:01:21 because the URL is basically everything
  • 01:01:24 after our host and we just have
  • 01:01:27 localhost well nothing and that
  • 01:01:30 basically translates to localhost / if I
  • 01:01:34 had slash test now we see another output
  • 01:01:37 and there we see slash test being locked
  • 01:01:41 here and then also get for the method
  • 01:01:44 and our headers so this is basically how
  • 01:01:47 we can access some information about our
  • 01:01:50 request
  • 01:01:50 now one crucial thing that is missing
  • 01:01:53 here is the response so let's have a
  • 01:01:56 look at sending responses in the next
  • 01:01:58 lecture
  • 01:02:03 in the last lecture you saw how to
  • 01:02:05 handle requests and how to read some
  • 01:02:07 data from the request like for example
  • 01:02:10 the URL and which HTTP method we used
  • 01:02:13 now we'll learn about different HTTP
  • 01:02:16 methods throughout the course by the way
  • 01:02:17 get is to default method used if you
  • 01:02:20 just enter a URL into your browser
  • 01:02:23 let's now shrink this again let's quit
  • 01:02:25 the server you always need to quit and
  • 01:02:27 restart if you edit it because otherwise
  • 01:02:29 your changes will not be reflected
  • 01:02:31 because the old process will still be
  • 01:02:33 running and let's now also use that
  • 01:02:35 response object now we could lock that
  • 01:02:38 with the console but actually this does
  • 01:02:40 not hold any interesting data instead we
  • 01:02:43 can use it to fill it with data we want
  • 01:02:46 to send back we do this by calling rest
  • 01:02:49 and now what there are a couple of
  • 01:02:53 methods we can use for example set
  • 01:02:54 header this allows us to set a new
  • 01:02:56 header for example content – type and
  • 01:03:01 that is a default header which the
  • 01:03:03 browser knows and understands and
  • 01:03:04 accepts and then as a second value here
  • 01:03:07 as a second argument and set header we
  • 01:03:09 set the value for this header key and we
  • 01:03:13 can send this to or set this to text
  • 01:03:15 HTML now what this will do is it will
  • 01:03:17 attach a header to our response where we
  • 01:03:21 basically pass a meta information saying
  • 01:03:24 that the type of the content which will
  • 01:03:27 also be part of the response is HTML now
  • 01:03:30 there is of course only a certain set of
  • 01:03:32 supported headers the browser
  • 01:03:34 understands and after this lecture
  • 01:03:36 you'll find an hour later with some link
  • 01:03:38 where you can learn which headers you
  • 01:03:41 can set now you don't need to set that
  • 01:03:44 many let me say that and later we will
  • 01:03:46 even learn about a package that does
  • 01:03:48 this for us so that we don't have to set
  • 01:03:50 the content type on our own but here I
  • 01:03:53 will set it now one important thing is
  • 01:03:55 missing of course and that is the HTML
  • 01:03:58 code right thus far I'm saying we have
  • 01:04:00 HTML code but I'm not sending it now we
  • 01:04:03 do this by setting response and now we
  • 01:04:06 can set right here right allows us to
  • 01:04:10 write some data cue the response and
  • 01:04:13 this basically works in in chunks you
  • 01:04:15 could say
  • 01:04:16 were in multiple lines this would be a
  • 01:04:18 good picture to look at this you write
  • 01:04:21 multiple lines of response for example
  • 01:04:23 if we write HTML code like this rests
  • 01:04:27 right and if this looks super strange
  • 01:04:31 now it is we'll learn about a way easier
  • 01:04:33 way of sending HTML later no worries so
  • 01:04:36 here I'm just sending HTML and you can
  • 01:04:38 also put longer HTML in there of course
  • 01:04:41 you could now have your your head where
  • 01:04:44 you set title my first page like this
  • 01:04:51 and you could put even more into that
  • 01:04:54 line but to make it easier to read I'll
  • 01:04:56 write a new line where I now have my
  • 01:04:59 body is I'm basically writing a whole
  • 01:05:02 HTML document here in a very complex way
  • 01:05:05 and there I'll just add h1 tag saying
  • 01:05:08 hello from my node.js server okay so
  • 01:05:13 this is now some HTML code and it will
  • 01:05:16 be written to the response line by line
  • 01:05:18 we now also need to tell note once we're
  • 01:05:21 done with creating that response we do
  • 01:05:24 this by calling end so after we set all
  • 01:05:27 the headers and wrote all the data to
  • 01:05:29 the response body we call end and now
  • 01:05:32 it's the point where we must not write
  • 01:05:35 anymore we can still call right but this
  • 01:05:38 will result in an error because we must
  • 01:05:40 not change the response after we ended
  • 01:05:43 it because this is basically the part
  • 01:05:44 where we will send it back to the client
  • 01:05:47 no js' we'll send it back to the client
  • 01:05:50 so here it should send back a response
  • 01:05:52 with some HTML code inside of it where
  • 01:05:56 we also tell the browser that it's HTML
  • 01:05:58 code the browser wouldn't know otherwise
  • 01:06:00 and with that if we save that file make
  • 01:06:04 sure you never forget to save your
  • 01:06:06 changes and weary execute it we again
  • 01:06:09 have that running process and now if I
  • 01:06:13 reload my localhost 3,000 page here we
  • 01:06:17 see hello from my noches server and if I
  • 01:06:20 open the chrome developer tools here
  • 01:06:22 which you can also do from the menu I
  • 01:06:25 use the shortcut you can also use view
  • 01:06:29 developer div
  • 01:06:30 / tools or that shortcut you see here
  • 01:06:32 now if you do that let me reload in the
  • 01:06:36 network tab here you will see this
  • 01:06:39 request this first request and there you
  • 01:06:42 see headers like for example in response
  • 01:06:45 headers there you see my content type
  • 01:06:48 which I said right this is the header we
  • 01:06:51 set here and if we click on response
  • 01:06:55 itself to see the response body we see
  • 01:06:58 that HTML document code we wrote with
  • 01:07:00 the head section with my first page and
  • 01:07:03 so on so this is now how we can send a
  • 01:07:07 response will later also learn about a
  • 01:07:11 way simpler way of doing that by using
  • 01:07:13 the Express J's framework but it's super
  • 01:07:16 important that you understand all the
  • 01:07:18 nitty-gritty details that go on behind
  • 01:07:21 the scenes and in this case we simply
  • 01:07:23 understand it by writing all the
  • 01:07:25 nitty-gritty code on our own this is how
  • 01:07:27 we can work with requests and send
  • 01:07:30 responses now let's connect both the
  • 01:07:32 request data we can get and the response
  • 01:07:35 data we can send
  • 01:07:40 in the last lectures we learned how to
  • 01:07:42 spin up that server and that we got a
  • 01:07:45 request object with information about
  • 01:07:47 the incoming requests and the response
  • 01:07:49 object that we can use to send back a
  • 01:07:51 response let's now connect both requests
  • 01:07:54 and response instead of printing some
  • 01:07:56 request data to the console which of
  • 01:07:58 course doesn't do much for us let's
  • 01:08:00 instead start writing a very simple web
  • 01:08:03 server that does different things
  • 01:08:05 depending on which route we enter so
  • 01:08:08 depending on which slash whatever part
  • 01:08:10 we enter here so let's say for slash
  • 01:08:14 nothing we want to load a page where the
  • 01:08:17 user can enter some data which we then
  • 01:08:21 store in a file on the server once it is
  • 01:08:24 sent we can do this by first of all
  • 01:08:28 parsing the URL I'm storing it in a new
  • 01:08:32 constant and I do this by accessing
  • 01:08:35 request URL remember that was something
  • 01:08:38 like slash slash test whatever we
  • 01:08:40 entered
  • 01:08:41 I will then add an if statement and
  • 01:08:43 check if URL is equal to just slash and
  • 01:08:47 only this will match by the way the
  • 01:08:50 triple equal sign means that this will
  • 01:08:51 only be true if URL is both a string and
  • 01:08:55 has that value so now here if that is
  • 01:09:01 the case I want to return a response
  • 01:09:03 which holds some HTML that gives the
  • 01:09:06 user an input form and a button that
  • 01:09:09 will send a new request in return and
  • 01:09:11 that will not be a get request by the
  • 01:09:14 way so let's see that step by step let's
  • 01:09:16 copy this code here and put it into this
  • 01:09:20 if statement and here I will write a
  • 01:09:23 HTML document with a head maybe a
  • 01:09:26 different title enter message and the
  • 01:09:30 body will now not hold a h1 tag but
  • 01:09:34 instead a form this is a default HTML
  • 01:09:38 element of course with a input of type
  • 01:09:41 text let's say and a button and this
  • 01:09:46 will be super ugly because we have no
  • 01:09:48 styling but it's about the functionality
  • 01:09:50 for now the button tag must be closed by
  • 01:09:53 the way
  • 01:09:54 the button will be of type submit' so
  • 01:09:57 that it submits the form and that will
  • 01:09:59 be some default HTML behavior we were
  • 01:10:01 using here where a button with type
  • 01:10:03 submit' in a form element will send a
  • 01:10:06 new request and we'll configure that
  • 01:10:08 request in a second let's first give the
  • 01:10:11 button a caption send and now on that
  • 01:10:14 form element here we add a action which
  • 01:10:17 is basically the URL this request which
  • 01:10:20 will be generated automatically should
  • 01:10:22 be sent to and I will use slash message
  • 01:10:26 here and this will automatically target
  • 01:10:29 the host it's running on so localhost in
  • 01:10:31 our case here localhost 3000 to be
  • 01:10:34 precise and then we define the method
  • 01:10:37 the HTTP method that should be used and
  • 01:10:39 there we previously saw if we expand
  • 01:10:43 this that we got a get request which is
  • 01:10:45 the default if we enter a URL well here
  • 01:10:48 we're not entering a URL instead we want
  • 01:10:51 to send a so called post request there
  • 01:10:54 is a limited set of HTTP words which you
  • 01:10:57 can use get and post are the two most
  • 01:11:00 important ones a get request is
  • 01:11:03 automatically sent when you click a link
  • 01:11:05 or enter a URL a post request has to be
  • 01:11:09 set up by you by creating such a form
  • 01:11:11 there also are some other ways by using
  • 01:11:14 JavaScript but we'll ignore them for now
  • 01:11:16 so in HTML we create such a form and we
  • 01:11:19 define that the method should be post
  • 01:11:20 and this will send a post request to
  • 01:11:23 slash message and the cool thing about
  • 01:11:25 form is it will not just send such a
  • 01:11:27 request it will also look into the form
  • 01:11:30 detect any inputs or related elements
  • 01:11:34 like selects we might have and if we
  • 01:11:37 give that input a name which we should
  • 01:11:39 message it will also automatically put
  • 01:11:43 that message into the request it sends
  • 01:11:46 to our server so now here when we wizard
  • 01:11:50 just localhost 3000 slash nothing we
  • 01:11:54 will return a response where we render
  • 01:11:56 this HTML code now let's also put a
  • 01:12:00 return statement in front of res end
  • 01:12:02 this is not required to return a
  • 01:12:05 response but to return
  • 01:12:08 from this anonymous function and to not
  • 01:12:10 continue this code because we returned
  • 01:12:12 prior to it and this will quit the
  • 01:12:15 function execution and we must do this
  • 01:12:17 because I told you that after rest end
  • 01:12:19 we must not call any other rest riots or
  • 01:12:23 arrests set headers but this would
  • 01:12:25 happen if we not return because then it
  • 01:12:28 would just continue execution with these
  • 01:12:29 lines I don't want that if we make it
  • 01:12:32 into the if statement we should also
  • 01:12:34 quit here we should exit out of this
  • 01:12:36 function with this let's restart the
  • 01:12:39 server by quitting it with ctrl C and
  • 01:12:42 restarting it with node API ass and
  • 01:12:45 let's reload this page on localhost 3000
  • 01:12:49 slash nothing and we see my input and
  • 01:12:54 the send button here again not super
  • 01:12:58 pretty but it's working now let's make
  • 01:13:01 sure that if we enter something we had
  • 01:13:03 sent we see this that we now not only
  • 01:13:07 see this but that something else happens
  • 01:13:09 by the way we do see this because now
  • 01:13:12 the URL is slash message and slash
  • 01:13:16 message does not make it into the safe
  • 01:13:18 statement and therefore disk code runs
  • 01:13:20 but we want to do something else so
  • 01:13:22 let's do that in the next lectures
  • 01:13:28 so we made sure that we listened to
  • 01:13:31 requests to just slash nothing and that
  • 01:13:34 we returned some HTML code with our
  • 01:13:36 input field on it now when we click that
  • 01:13:39 send button we send a post request to
  • 01:13:41 slash message but we're not doing
  • 01:13:43 anything with that time to change that
  • 01:13:45 let's add an average statement here and
  • 01:13:48 let's check if the URL is equal to
  • 01:13:51 message and let's add an average
  • 01:13:53 condition and that condition is that I
  • 01:13:55 want to be sure that we're not handling
  • 01:13:58 a get request at a post request here so
  • 01:14:01 let's also parse the method from request
  • 01:14:05 method and make sure that method is
  • 01:14:09 equal to post now we'll only enter this
  • 01:14:13 if statement if we have a post request
  • 01:14:15 to slash message which happens to be
  • 01:14:18 exactly what we create with this form in
  • 01:14:20 this case I want to do two things
  • 01:14:24 I wanna redirect the user back to slash
  • 01:14:30 nothing so not leave him on slash
  • 01:14:32 message and I want to create a new file
  • 01:14:36 and stored a message to user entered in
  • 01:14:39 it now this involves a couple of things
  • 01:14:42 let's start with redirecting and
  • 01:14:44 creating that file we actually already
  • 01:14:47 worked with a file in the first course
  • 01:14:49 section and do you remember how dad
  • 01:14:52 worked
  • 01:14:53 feel free to go along on your own if you
  • 01:14:56 know that we need another package
  • 01:15:00 another core module and that was the
  • 01:15:03 file system core module so let's import
  • 01:15:06 it by storing the functionality in a
  • 01:15:08 constant you can name it however you
  • 01:15:10 want I'll name it FS because the package
  • 01:15:13 has named FS but you can also name this
  • 01:15:15 differently at this not the package just
  • 01:15:18 a constant so FS allows us to work with
  • 01:15:22 the file system and here I now want to
  • 01:15:25 write a new file so here let's execute
  • 01:15:28 write file and write file takes a path
  • 01:15:32 to the file and I'll just use the
  • 01:15:34 filename to create it in the same folder
  • 01:15:36 as fjs and i'll simply name it message
  • 01:15:40 dot txt
  • 01:15:41 and in there I obviously want to store
  • 01:15:44 what the user sent now this is a little
  • 01:15:45 bit more work so for now let's just put
  • 01:15:48 some dummy text in there and let's
  • 01:15:50 redirect the user now important we
  • 01:15:53 should actually use write file sync for
  • 01:15:56 now and I'll explain what the difference
  • 01:15:57 to write file which also exists is in a
  • 01:16:01 while so let's use write file sync for
  • 01:16:03 now now right head which basically
  • 01:16:07 allows us to write some meta information
  • 01:16:10 in one go and then we set a status code
  • 01:16:13 of 302 which stands for redirection and
  • 01:16:16 then we've has a JavaScript object with
  • 01:16:19 some headers we want to set and you
  • 01:16:21 could also do this in two steps by the
  • 01:16:22 way you can also set the status code to
  • 01:16:26 two 302 and then simply have loops set
  • 01:16:30 header and there we set the location
  • 01:16:33 this is also a default header accepted
  • 01:16:36 by the browser and we set that location
  • 01:16:38 to just slash and it will automatically
  • 01:16:40 use the host we're already running on
  • 01:16:42 and then we have to call rest and and
  • 01:16:45 important as before return this so that
  • 01:16:49 we don't execute these lines otherwise
  • 01:16:51 we'll get an error
  • 01:16:53 now let's restart our file here and make
  • 01:16:58 sure to go back to slash nothing and
  • 01:17:01 reload that page with the input field
  • 01:17:03 and send any value and you should simply
  • 01:17:06 reload that in the end because you get
  • 01:17:08 redirected here but you can see that
  • 01:17:10 redirect here in the network tab of the
  • 01:17:12 developer tools here 302 indicates we
  • 01:17:15 send a request to message and we were
  • 01:17:17 redirected to localhost so this is
  • 01:17:20 working and we also get the message text
  • 01:17:22 with dummy inside of it now let's also
  • 01:17:25 get rid of that though and let's work on
  • 01:17:30 actually parsing the data the user sent
  • 01:17:32 us and writing dead data into that file
  • 01:17:39 so time to parse the incoming requests
  • 01:17:42 and gets the data that is part of the
  • 01:17:45 request because that data should be
  • 01:17:47 whatever we entered here now how do we
  • 01:17:50 get access to that well we got request
  • 01:17:53 URL and request method you might think
  • 01:17:56 there is something like request data but
  • 01:17:59 there isn't
  • 01:18:00 instead the incoming data is basically
  • 01:18:03 sent as a stream of data and there is a
  • 01:18:06 special construct javascript in general
  • 01:18:09 knows but no js' uses a lot now what is
  • 01:18:12 such a stream of data though there is a
  • 01:18:15 connected concept buffers and we'll have
  • 01:18:17 a look at both here so let's take our
  • 01:18:20 incoming request as an example there
  • 01:18:22 also are other streams like for example
  • 01:18:24 when working with files we can also work
  • 01:18:26 with streams but let's stick to requests
  • 01:18:28 here our stream here is basically an
  • 01:18:31 ongoing process the request is simply
  • 01:18:34 read by note in chunks you could say in
  • 01:18:38 multiple parts and in the ends at some
  • 01:18:41 point of time it's done and this is done
  • 01:18:43 so that we theoretically can start
  • 01:18:46 working on this on the individual chunks
  • 01:18:48 without having to wait for the full
  • 01:18:50 request being read now for a simple
  • 01:18:53 request like the one we're working with
  • 01:18:55 this is not really required we only got
  • 01:18:58 one input field data it doesn't take so
  • 01:19:01 long to parse that but consider a file
  • 01:19:04 being uploaded this will take
  • 01:19:06 considerably longer and therefore
  • 01:19:08 streaming that data could make sense
  • 01:19:11 because it could allow you to start
  • 01:19:13 writing this to your disk so to your
  • 01:19:16 hard drive where your app runs your note
  • 01:19:19 app runs on your server was the data is
  • 01:19:22 coming in so that you don't have to
  • 01:19:24 parse the entire file which is of course
  • 01:19:27 taking some time and you have to wait
  • 01:19:29 for it being fully uploaded before you
  • 01:19:31 can do anything with it but this is how
  • 01:19:33 node handles all requests because it
  • 01:19:36 doesn't know in advance how complex and
  • 01:19:38 big they are so you can start working on
  • 01:19:42 the data earlier the problem is with
  • 01:19:44 your code you can't arbitrarily try to
  • 01:19:47 work with these chunks instead
  • 01:19:51 you organize these incoming chunks you
  • 01:19:54 use a so called buffer a buffer is like
  • 01:19:57 a bus stop if you consider buses they're
  • 01:20:00 always driving but for users or
  • 01:20:04 customers being able to work with them
  • 01:20:06 to climb on the bus and leave the bus
  • 01:20:09 you need bus stops where you well you
  • 01:20:12 can interact with the bus basically and
  • 01:20:14 that is what a buffer is a buffer is
  • 01:20:16 simply a construct which allows you to
  • 01:20:19 hold multiple chunks and work with them
  • 01:20:23 before they are released once you're
  • 01:20:25 done and you work with that buffer now
  • 01:20:29 that's pretty abstract but it's pretty
  • 01:20:30 easy to work with fortunately so let's
  • 01:20:33 see how it works in practice when
  • 01:20:35 receiving a posted message before
  • 01:20:37 sending the response and before writing
  • 01:20:40 to the file we want to get our request
  • 01:20:42 data right and we do this by going to
  • 01:20:45 our request and registering an event
  • 01:20:49 listener we haven't done that thus far
  • 01:20:52 but as I mentioned know'd uses these
  • 01:20:54 heavily for a create server it
  • 01:20:56 implicitly creates one for us now we do
  • 01:20:59 this on our own
  • 01:21:00 by using the on method now on allows us
  • 01:21:04 to listen to certain events and the
  • 01:21:07 event I want to listen to here is the
  • 01:21:09 data event you see my IDE even gives me
  • 01:21:12 some help here and tells me which events
  • 01:21:14 I can listen to for a request so here I
  • 01:21:17 want to listen for the data event the
  • 01:21:20 data event will be fired whenever a new
  • 01:21:23 chunk is ready to be read you remember
  • 01:21:26 that buffer thing this is basically
  • 01:21:28 helping us with that now here we have to
  • 01:21:33 add a second argument which is that
  • 01:21:34 function that should be executed for
  • 01:21:37 every data event you remember a create
  • 01:21:39 server it has a similar concept there we
  • 01:21:42 define a function that should be
  • 01:21:43 executed for every incoming request now
  • 01:21:46 we're defining a function to be executed
  • 01:21:48 for every incoming data piece so here
  • 01:21:51 I'll again use a es6 arrow function you
  • 01:21:54 could always use the function keyword
  • 01:21:56 without that arrow then and as you can
  • 01:21:59 also see on data this listener receives
  • 01:22:03 a chance
  • 01:22:04 of data so here we receive a chunk and
  • 01:22:07 this chunk is something we can work with
  • 01:22:10 here and now we have to do something
  • 01:22:12 with this chunk to be able to interact
  • 01:22:14 with it for this I will create a new
  • 01:22:18 constant here and I'll name it body
  • 01:22:22 because I'll try to read the request
  • 01:22:24 body you can name it however you want
  • 01:22:26 but it is to request body we're working
  • 01:22:29 with now the body should be an empty
  • 01:22:32 array and now in that function here in
  • 01:22:36 the data event I'll take my body and
  • 01:22:38 push a new element onto it by the way if
  • 01:22:42 you're wondering how we can edit a
  • 01:22:44 constant value this only means that we
  • 01:22:47 can never really value so we can never
  • 01:22:51 use body equals something again but with
  • 01:22:54 push we're changing the object behind
  • 01:22:57 that body element that body object we're
  • 01:23:01 editing the data in that object not the
  • 01:23:04 value itself not the object itself it's
  • 01:23:07 a bit strange to wrap your head around
  • 01:23:08 but this is in the end how it works so
  • 01:23:11 we can now push a new element into this
  • 01:23:14 array to make it non-empty and we push
  • 01:23:16 our chunk here now nodejs will execute
  • 01:23:20 this so often until it's done getting
  • 01:23:22 all the data out of our request that can
  • 01:23:24 be ones that can be multiple times and
  • 01:23:27 we can even lock this to see how how
  • 01:23:29 often it does this and what's inside of
  • 01:23:31 this chunk now we need to register an
  • 01:23:35 aviary event listener and that is d and
  • 01:23:37 listener this will be fired once it's
  • 01:23:40 done parsing the incoming request data
  • 01:23:42 or the incoming requests in general here
  • 01:23:45 it will again execute a function we
  • 01:23:47 define as a second argument and in this
  • 01:23:50 function we can now rely on all the
  • 01:23:53 chunks being read in and they're all
  • 01:23:55 stored in the body now now to interact
  • 01:23:59 with this and don't forget to come after
  • 01:24:02 end to interact with that to work with
  • 01:24:06 all these chunks we now need to buffer
  • 01:24:09 them remember that bus stop concept we
  • 01:24:12 got all these chunks we now need to do
  • 01:24:14 something to be able to work with them
  • 01:24:16 to basically have one
  • 01:24:18 the bus stops and we can interact with
  • 01:24:21 it so here I'll now create a new
  • 01:24:24 constant parsed body and there I will
  • 01:24:28 use the buffer object which is available
  • 01:24:30 globally made available by nodejs and I
  • 01:24:34 can concat my body so this will in the
  • 01:24:38 end create a new buffer and add all the
  • 01:24:40 chunks from inside my body to it and
  • 01:24:42 then on this buffer which I got here
  • 01:24:45 parsed body is now a buffer there I can
  • 01:24:49 call to string to convert it to a string
  • 01:24:52 so this is a utility method offered by
  • 01:24:54 nodejs
  • 01:24:54 where we do something to our buffered
  • 01:24:57 chunks remember the bus is now waiting
  • 01:25:00 and the bus stops so to say the buffer
  • 01:25:03 is our bus stop and now we do something
  • 01:25:05 with it
  • 01:25:06 here we convert it to a string and this
  • 01:25:08 only works because I know that the
  • 01:25:10 incoming data will be text because the
  • 01:25:13 body of that request will be text if it
  • 01:25:16 were a file we would have to do
  • 01:25:18 something different but it is no file
  • 01:25:20 and I know that it isn't because we're
  • 01:25:22 writing the code we know what we will
  • 01:25:24 receive right so this is the parsed body
  • 01:25:26 and this is now finally when we can work
  • 01:25:30 with so let's also output the parsed
  • 01:25:33 body this was a lot of talking so let's
  • 01:25:36 simply have a look with the server with
  • 01:25:38 control C and restart it and then send
  • 01:25:43 another request with some message here
  • 01:25:45 and now if you have a look at what's
  • 01:25:48 being locked you see two elements the
  • 01:25:50 first one is coming from this console
  • 01:25:52 log and you see that is a chunk that is
  • 01:25:55 a chunk we can't work with but now the
  • 01:25:57 parse body really sees or yields this
  • 01:26:01 line and that is something we can work
  • 01:26:03 with and it's message equals something
  • 01:26:05 because we named our input here message
  • 01:26:10 and as I said that forum will
  • 01:26:12 automatically send a request where it
  • 01:26:14 takes all the input data and puts it
  • 01:26:16 into a request body as key value pairs
  • 01:26:20 where the names assigned to the inputs
  • 01:26:22 are the keys and the values are what the
  • 01:26:24 user entered and that is what we have
  • 01:26:26 here a key value pair separating the key
  • 01:26:30 from the value with an equal
  • 01:26:31 sign now and with that we can now work
  • 01:26:35 with that and finally store the input in
  • 01:26:39 our file and we can do that here and
  • 01:26:43 request on and request on and to be
  • 01:26:45 precise by creating a new constant
  • 01:26:48 message taking the parsed body and
  • 01:26:52 splitting it on the equal sign and then
  • 01:26:55 taking the element with the index one
  • 01:26:57 which is the second element in the
  • 01:26:59 resulting array which is the element on
  • 01:27:01 the right of the equal sign and now we
  • 01:27:03 can move right file sync into the end
  • 01:27:06 function we don't want to execute it
  • 01:27:09 here because this will actually run
  • 01:27:11 before this function is called because
  • 01:27:14 here we just register a function to be
  • 01:27:16 called in the future it's not executed
  • 01:27:19 immediately
  • 01:27:19 remember note doesn't wait and pause it
  • 01:27:22 will not block the script execution it
  • 01:27:25 just registers this as a to be executed
  • 01:27:28 action and then right away continues so
  • 01:27:31 if we have something that depends on the
  • 01:27:33 incoming data we have to move it into
  • 01:27:35 the event listener to so that it's all
  • 01:27:38 the part of the to be executed code
  • 01:27:40 sometimes in the future and doesn't run
  • 01:27:42 too early and now we can write message
  • 01:27:47 to the message to text file
  • 01:27:49 let's now restart this file one more
  • 01:27:51 time and enter hello here and hit send
  • 01:28:00 and now if you look into message text we
  • 01:28:03 see hello you see the exclamation mark
  • 01:28:05 was encoded now that is something we can
  • 01:28:08 worry about later but in general this
  • 01:28:10 worked just fine and if you are now
  • 01:28:13 totally frightened by how complex nodejs
  • 01:28:17 is this is the raw logic will later use
  • 01:28:20 Express J's which hides all that raw
  • 01:28:23 logic but to understand why we use that
  • 01:28:26 you first of all need to understand what
  • 01:28:28 is happening and why using tools like
  • 01:28:31 expressed chess which will make all of
  • 01:28:33 this much easier are great so with that
  • 01:28:37 we got our basic logic down let's now
  • 01:28:40 dive again into that event listener and
  • 01:28:43 writing files thing
  • 01:28:45 because there is something really
  • 01:28:46 important you have to understand
  • 01:28:52 we already achieved a lot in this
  • 01:28:54 section and I know that all this code
  • 01:28:57 looks kind of intimidating and no
  • 01:29:00 worries it will become much easier but I
  • 01:29:03 find it super important to learn it the
  • 01:29:05 hard way first so that you never forget
  • 01:29:07 what's happening behind the scenes
  • 01:29:10 now one crucial thing which I know that
  • 01:29:13 people often struggle with is that the
  • 01:29:16 order of execution of your code here is
  • 01:29:19 not necessarily the order in which you
  • 01:29:22 write it for example this here will
  • 01:29:26 actually execute after this code so it
  • 01:29:29 will even execute after we already sent
  • 01:29:32 a response this has two important
  • 01:29:34 implications for one sending the
  • 01:29:37 response does not mean that our event
  • 01:29:40 listeners here are dead they will still
  • 01:29:42 execute even if the response is already
  • 01:29:44 gone but it also means that if we do
  • 01:29:47 something in the event listener that
  • 01:29:49 should influence in the response this is
  • 01:29:51 the wrong way of setting it up we should
  • 01:29:53 then also move the response code into
  • 01:29:55 the event listener if we had such a
  • 01:29:57 dependency but it also means that it's
  • 01:30:00 super important to understand that with
  • 01:30:03 requests on or code like HTTP create
  • 01:30:07 server these are some examples where
  • 01:30:10 nodejs uses a pattern where you pass a
  • 01:30:13 function to a function and node will
  • 01:30:16 execute these passed in functions at a
  • 01:30:19 later point of time which is called
  • 01:30:22 asynchronously now it's not always the
  • 01:30:24 case that a passed in function is
  • 01:30:26 necessarily executed at a later point of
  • 01:30:29 time but no js' used this pattern
  • 01:30:31 heavily and throughout the course I'll
  • 01:30:33 of course let you know when this is the
  • 01:30:35 case and when node execute something
  • 01:30:37 asynchronously in such cases no chairs
  • 01:30:43 won't immediately run that function
  • 01:30:46 instead what it does when it first
  • 01:30:48 encounters this line is it will simply
  • 01:30:51 add a new event listener internally it
  • 01:30:54 manages all these listeners internally
  • 01:30:56 in this case for the end event on the
  • 01:31:00 request which will be triggered
  • 01:31:01 automatically once nodejs is done
  • 01:31:03 parsing the request so this
  • 01:31:05 something noches does for you and it
  • 01:31:08 will then call that function for you
  • 01:31:11 once it is done so in the end you can
  • 01:31:13 think of this like nodejs having some
  • 01:31:16 internal registry of events and
  • 01:31:19 listeners to these events and a function
  • 01:31:21 like this is such a listener and when
  • 01:31:24 sort of something happens so when nodejs
  • 01:31:26 is done parsing your request it will go
  • 01:31:29 through that registry and see hmm
  • 01:31:32 I'm done with the request so I should
  • 01:31:34 now send the end event so let's see
  • 01:31:36 which listeners I have for that and it
  • 01:31:39 will then find this function and any
  • 01:31:42 other functions you might have
  • 01:31:43 registered for that and will now call
  • 01:31:45 them but it will not pause the average
  • 01:31:49 code execution and that is so important
  • 01:31:51 to understand so for example here now
  • 01:31:54 since I moved return response and into
  • 01:31:57 this function the flow is like this it
  • 01:32:00 will now reach this if statement and if
  • 01:32:03 these conditions are met it will go
  • 01:32:04 inside of it it will then register these
  • 01:32:08 two handlers and not immediately execute
  • 01:32:10 these two functions instead the
  • 01:32:12 functions are just registered internally
  • 01:32:15 in its event emitter registry and then
  • 01:32:18 it will jump straight away to the next
  • 01:32:20 line and therefore right now if I would
  • 01:32:24 restart my server save the code and
  • 01:32:26 restart my server here you will see that
  • 01:32:29 if I enter something here I actually get
  • 01:32:32 redirected to dispatch or not even
  • 01:32:34 redirected as you can see there is no
  • 01:32:36 300 status code instead it just loads
  • 01:32:39 this page because it executes these
  • 01:32:42 lines because as a now mentioned
  • 01:32:44 multiple times it will not execute this
  • 01:32:46 right away and this return statement
  • 01:32:48 will therefore not quit this overarching
  • 01:32:51 function here instead it just registers
  • 01:32:55 this callback and immediately moves on
  • 01:32:57 to the next lines and it would
  • 01:32:59 eventually execute this line but that is
  • 01:33:01 already too late which is also why we
  • 01:33:03 get D cannot set headers error here
  • 01:33:06 because it already moved along and
  • 01:33:08 executed this code when all of a sudden
  • 01:33:10 the parsing of the request finished and
  • 01:33:12 it executed this code and tried to again
  • 01:33:15 send a response which obviously is too
  • 01:33:17 late because it already did here
  • 01:33:19 now I know that this is hard to wrap
  • 01:33:23 your head around but it is a crucial
  • 01:33:25 concept that you can register code
  • 01:33:28 functions which run sometime in the
  • 01:33:31 future but not necessarily right now and
  • 01:33:35 therefore the next line of code this
  • 01:33:37 code here can run or will run before
  • 01:33:40 this code simply because this is just a
  • 01:33:43 callback to be called sometime in the
  • 01:33:46 future and this setup is important
  • 01:33:49 because otherwise node would have to
  • 01:33:51 pause until it's done
  • 01:33:53 pause until it wrote the file and if it
  • 01:33:56 does that it will simply slow our server
  • 01:33:59 down and it's not able to handle our
  • 01:34:01 incoming requests or do anything of that
  • 01:34:04 kind until it's done and that is not
  • 01:34:07 what we want we don't want to block our
  • 01:34:10 code execution we always want to be in
  • 01:34:12 that wait for a new events loop the
  • 01:34:15 event loop and then only execute code
  • 01:34:17 once it's due to be executed and never
  • 01:34:19 block that event loop for too long of a
  • 01:34:22 time and this is why we have this setup
  • 01:34:24 and this has one implication for this
  • 01:34:27 line and for this line the implication
  • 01:34:30 for this line is that we reach it too
  • 01:34:32 early so to avoid this we should
  • 01:34:34 actually return here we simply return
  • 01:34:38 request on so that this gets executed
  • 01:34:40 but the line thereafter doesn't and the
  • 01:34:43 important application about this line
  • 01:34:45 will be discussed in the next lecture
  • 01:34:52 so what is wrong with this right file
  • 01:34:55 syncline here well it's desync keyword i
  • 01:35:00 earlier mentioned that there also is a
  • 01:35:02 right file method but we used right file
  • 01:35:06 sync the sync here stands for
  • 01:35:09 synchronous and this is a special method
  • 01:35:13 which will actually block code execution
  • 01:35:16 until this file is created now working
  • 01:35:20 with files is available in two modes
  • 01:35:23 here is the synchronous mode we block
  • 01:35:26 execution of the next line of code until
  • 01:35:28 this file is done now for this short of
  • 01:35:32 a text we enter this is super fast and
  • 01:35:35 we won't even notice it but think about
  • 01:35:37 a huge file a couple of hundreds of
  • 01:35:41 megabytes or even bigger if you do
  • 01:35:43 something with that read it copy it
  • 01:35:46 whatever it is and you block the code
  • 01:35:49 execution then the next line and all the
  • 01:35:52 other code will not continue to run
  • 01:35:54 until that file operation is done and
  • 01:35:57 even new incoming requests of our users
  • 01:36:00 would not be handled until that file
  • 01:36:03 operation is done and you don't want
  • 01:36:06 that therefore you should not use that
  • 01:36:08 syntax here you can if you know you'll
  • 01:36:11 only have a very short file operation
  • 01:36:13 but even then it's better to use this
  • 01:36:16 write file method here which actually
  • 01:36:19 does not just accept a path and the data
  • 01:36:22 but also a third argument and that is
  • 01:36:25 again such a callback so a function that
  • 01:36:28 should be executed when it's done so
  • 01:36:32 here again just as with create server
  • 01:36:34 nodejs
  • 01:36:36 implicitly registers an event listener
  • 01:36:38 for us
  • 01:36:39 so here I've has another function and
  • 01:36:43 this callback here actually receives an
  • 01:36:47 error object which will be null if no
  • 01:36:51 error occurred but if some error
  • 01:36:53 occurred missing permissions or anything
  • 01:36:55 like that you would get it here and you
  • 01:36:57 could then handle it gracefully by
  • 01:37:00 returning a different kind of response a
  • 01:37:02 error response showing to the user that
  • 01:37:05 an error occur
  • 01:37:05 and otherwise return a normal response
  • 01:37:08 now here I won't do error handling and
  • 01:37:11 there is really nothing that could go
  • 01:37:12 wrong here well of course dive into
  • 01:37:14 error handling a bit more later in the
  • 01:37:16 course but I will remove my normal
  • 01:37:19 response code in there because this
  • 01:37:21 response should only be sent if we're
  • 01:37:24 done working with the file because that
  • 01:37:27 is ultimately team action we want to do
  • 01:37:30 up on our request right so now we
  • 01:37:33 actually have our event listener with
  • 01:37:35 some method or function that will be
  • 01:37:38 executed once we're done parsing the
  • 01:37:40 request and in that function that will
  • 01:37:42 be executed sometime in the future we
  • 01:37:44 have yet another event listener this
  • 01:37:47 nested function here which will be
  • 01:37:49 executed once we're done writing the
  • 01:37:50 file and this is actually pretty
  • 01:37:53 standard for nodejs you have this
  • 01:37:56 event-driven architecture where you
  • 01:37:59 basically tell no choice please do
  • 01:38:02 something and it will then go ahead and
  • 01:38:04 offload that process to the operating
  • 01:38:06 system which does use multi-threading
  • 01:38:08 and so on and will then continue its
  • 01:38:10 event loop to listen for event callbacks
  • 01:38:13 and always just dispatch tiny actions
  • 01:38:17 like that to never block the code
  • 01:38:19 execution and then always just come back
  • 01:38:22 once an operation is done by the
  • 01:38:24 operating system and so on so this is
  • 01:38:26 what noches does here and why it is high
  • 01:38:29 performant because it never blocks your
  • 01:38:31 code
  • 01:38:32 it never blocks the server it just goes
  • 01:38:35 ahead and tells the operating system do
  • 01:38:37 that do this and then eventually comes
  • 01:38:39 back and does something in the callback
  • 01:38:41 like send a response which is not a
  • 01:38:44 blocking operation because this is super
  • 01:38:46 fast just a couple of headers and off
  • 01:38:48 you go so I hope this is clear and with
  • 01:38:51 that let's restart this by quitting the
  • 01:38:54 server rerunning node app and going back
  • 01:38:58 to just slash nothing let's enter hello
  • 01:39:07 again here and send and in message text
  • 01:39:11 we see that again concatenated with an
  • 01:39:14 extra character not something we have to
  • 01:39:16 worry about right now so you see right
  • 01:39:18 file all the works but this is the way
  • 01:39:21 you should use it due to the
  • 01:39:23 asynchronous nature of no chairs which a
  • 01:39:25 hope now became clear because it is a
  • 01:39:28 crucial concept of no chairs and one you
  • 01:39:31 absolutely have to understand with that
  • 01:39:34 let's wrap this module up with these
  • 01:39:37 core basics and let's from now on move
  • 01:39:39 to a more easier route where we will
  • 01:39:42 make our life simpler and not write all
  • 01:39:45 this nitty-gritty detail killed on our
  • 01:39:47 own but it is super crucial that you
  • 01:39:50 understand what's going on here
  • 01:39:56 over the last lectures I extensively
  • 01:39:59 mentioned how node works with
  • 01:40:01 asynchronous code and I mentioned this
  • 01:40:04 event loop which is important let me
  • 01:40:06 dive deeper into that and deeper into
  • 01:40:10 how noches exactly execute your code to
  • 01:40:13 stay performant and still be able to
  • 01:40:16 handle long taking tasks like working
  • 01:40:19 with files or things like that
  • 01:40:21 because there is an important concept
  • 01:40:23 which you have to understand to
  • 01:40:25 understand well what is happening behind
  • 01:40:27 the scenes
  • 01:40:28 so in our node application we have our
  • 01:40:31 code and one important thing to
  • 01:40:33 understand and to really keep in mind is
  • 01:40:35 that note j/s uses only one single
  • 01:40:39 javascript fret a fret is basically like
  • 01:40:42 a process in your operating system you
  • 01:40:44 could say so it's only using that one
  • 01:40:47 thread and the obvious question is how
  • 01:40:49 is it then able to handle multiple
  • 01:40:51 requests because if we're not able to
  • 01:40:55 assign a new fret for each request they
  • 01:40:58 ultimately end up all running in one on
  • 01:41:01 the same fret and this of course poses a
  • 01:41:03 security question can you then access
  • 01:41:06 data from request a from request B and
  • 01:41:10 most importantly here at this point when
  • 01:41:12 we talk about performance there of
  • 01:41:14 course all the is the question of does
  • 01:41:17 does not mean that if the request a is
  • 01:41:19 still doing work requests B can't be
  • 01:41:22 handled well both is taken care of of
  • 01:41:25 nodejs
  • 01:41:25 and at this point of time here i want to
  • 01:41:28 focus on the performance question though
  • 01:41:30 i'll also briefly touch on the security
  • 01:41:33 question now let's start with the
  • 01:41:35 performance let's say we have some code
  • 01:41:37 which accesses the file system as we
  • 01:41:40 already did in this course to now
  • 01:41:42 working with files oft is a task that
  • 01:41:45 takes longer because files can be very
  • 01:41:48 big and it doesn't necessarily complete
  • 01:41:50 instantly therefore if we're doing this
  • 01:41:53 upon an incoming request a second
  • 01:41:55 request might have to wait because we're
  • 01:41:58 not able to handle it yet or it even
  • 01:42:00 gets a decline so basically our webpage
  • 01:42:03 is down for that user now one important
  • 01:42:06 construct I already mentioned is that
  • 01:42:08 event loop
  • 01:42:10 the event loop is automatically started
  • 01:42:12 by no chess when your program starts you
  • 01:42:15 don't have to do that explicitly no
  • 01:42:17 chess
  • 01:42:18 does that when well it basically starts
  • 01:42:20 running code this is responsible for
  • 01:42:23 handling event callbacks though so all
  • 01:42:27 these nice functions we basically added
  • 01:42:29 thus far in create server for example
  • 01:42:32 the event loop is responsible for
  • 01:42:35 basically running that code when a
  • 01:42:38 certain event occurs you could say it's
  • 01:42:40 aware of all these callbacks and
  • 01:42:41 basically well execute that code that
  • 01:42:44 doesn't help us with our long taking
  • 01:42:47 file operation though and it's important
  • 01:42:49 to understand that this operation is not
  • 01:42:52 handled by the event loop just the
  • 01:42:55 callback that we might have to find on
  • 01:42:58 write file once it's done dad code will
  • 01:43:02 be handled in the event loop but that
  • 01:43:03 code will finish fast so basically the
  • 01:43:06 event loop will only handle callbacks
  • 01:43:09 that contain fast finishing code instead
  • 01:43:14 our file system operation and a couple
  • 01:43:16 of other long tagging operations are
  • 01:43:18 sent to a worker pool which is also spun
  • 01:43:21 up and managed by no chess automatically
  • 01:43:24 this worker pool is responsible for all
  • 01:43:27 the heavy lifting this worker pool is
  • 01:43:30 kind of totally detached of your
  • 01:43:33 JavaScript code you could say and it
  • 01:43:36 runs on different frets it can spin up
  • 01:43:38 multiple frets it's closely intervened
  • 01:43:41 with your operating system you're
  • 01:43:42 running the app on so this is really
  • 01:43:44 detached from your code and this worker
  • 01:43:47 pool is therefore doing all the heavy
  • 01:43:49 lifting if you're doing something with a
  • 01:43:51 file well a worker from that pool will
  • 01:43:54 take care and will do its job totally
  • 01:43:57 detached from your code and from the
  • 01:43:59 request and from the event loop the one
  • 01:44:02 connection to the event loop we will
  • 01:44:04 have though is that once the worker is
  • 01:44:07 done so for example once we read a file
  • 01:44:10 it will trigger the callback for that
  • 01:44:13 read file operation and since the event
  • 01:44:16 loop is responsible for the events and
  • 01:44:18 the callbacks this will in the end end
  • 01:44:20 up in the event loop
  • 01:44:22 so they're noches will then basically
  • 01:44:25 execute the appropriate callback now
  • 01:44:28 this is a lot of behind the scenes stuff
  • 01:44:30 which is nice to know you don't have to
  • 01:44:32 write any code to make this work this is
  • 01:44:35 all built into node.js and if you write
  • 01:44:38 code as you learn it in this course you
  • 01:44:40 automatically take advantage of this I
  • 01:44:42 still of course want you to understand
  • 01:44:44 how that works behind the scenes because
  • 01:44:46 I believe that helps you understand
  • 01:44:48 nodejs better now let's look into that
  • 01:44:51 event loop then that event loop is in
  • 01:44:55 the end a loop which is run or started
  • 01:44:58 by noches
  • 01:44:59 that keeps the noches process running
  • 01:45:01 and as I just mentioned that handles all
  • 01:45:04 the callbacks and it has a certain order
  • 01:45:07 in which it goes fruity callbacks so
  • 01:45:10 basically it's a loop that just ball
  • 01:45:12 keeps on looping unsurprisingly at the
  • 01:45:15 beginning of each new iteration it
  • 01:45:17 checks if there are any timer callbacks
  • 01:45:20 it should execute we haven't set up any
  • 01:45:23 timers yet but basically there is set
  • 01:45:25 timeout and set interval you might
  • 01:45:28 notice from front-end JavaScript to
  • 01:45:30 there these methods also exist now in no
  • 01:45:33 chess you can also set a timer and
  • 01:45:35 basically you set a timer and always
  • 01:45:38 pass a method a function that should be
  • 01:45:41 executed once that timer completes and
  • 01:45:43 nodejs is aware of this and at the
  • 01:45:45 beginning of each new loop iteration it
  • 01:45:47 executes any do timer called X so any
  • 01:45:51 callbacks that have to be executed
  • 01:45:52 because of timer completes then as a
  • 01:45:55 next step it checks every callbacks for
  • 01:45:58 example with we had write or read file
  • 01:46:01 we might have a callback because that
  • 01:46:03 operation finished and it will then also
  • 01:46:05 execute these callbacks now be aware
  • 01:46:08 that with IO here I mean generally any
  • 01:46:11 input/output operations that typically
  • 01:46:15 is file operations but can also be
  • 01:46:17 network operations and in general I'm
  • 01:46:19 talking about blocking long taking
  • 01:46:22 operations now it's important to
  • 01:46:24 understand that no chess will leave that
  • 01:46:26 face at a certain point of time and that
  • 01:46:29 can also mean that if there are too many
  • 01:46:31 outstanding callbacks it will continue
  • 01:46:34 its loop iteration and postponed
  • 01:46:36 these callbacks to the next iteration to
  • 01:46:39 execute them after working on these open
  • 01:46:42 callbacks and hopefully finishing them
  • 01:46:45 all it will enter a paul face the paul
  • 01:46:48 face is basically a face where no chairs
  • 01:46:52 will look for new i/o events and
  • 01:46:55 basically do its best to execute their
  • 01:46:58 callbacks immediately if possible now if
  • 01:47:02 that is not possible it will defer the
  • 01:47:04 execution and basically register this as
  • 01:47:06 a pending callback so this is how that
  • 01:47:09 works important
  • 01:47:12 it also will check if there are any
  • 01:47:14 timer callbacks due to be executed and
  • 01:47:18 if that is the case it will jump to that
  • 01:47:20 timer face and execute them right away
  • 01:47:23 so it can actually jump back there and
  • 01:47:25 not finish the iteration otherwise it
  • 01:47:27 will continue and next set immediate
  • 01:47:31 callbacks will be executed in a
  • 01:47:33 so-called check face set immediate is a
  • 01:47:36 bit like set timeout or set interval
  • 01:47:38 just that it will execute immediately
  • 01:47:40 but always after any open callbacks have
  • 01:47:45 been executed so typically faster than
  • 01:47:48 set timeout with one millisecond of open
  • 01:47:51 duration let's say but after the current
  • 01:47:54 cycle well finished or at least finished
  • 01:47:57 open callbacks that were due to be
  • 01:47:59 handled in that current iteration and
  • 01:48:01 now we're entering highly theoretical
  • 01:48:03 terrain and I don't want to dive too
  • 01:48:06 deep into that though you will find a
  • 01:48:08 couple of resources that do dive deeper
  • 01:48:10 at the end of this module in case you
  • 01:48:13 want to join you know chess team and
  • 01:48:14 really dive super hardcore introduced
  • 01:48:17 now we're nearing the end of each
  • 01:48:19 iteration cycle and now no chess will
  • 01:48:22 execute all closed event callbacks so if
  • 01:48:25 you registered any closed events and in
  • 01:48:28 our code we haven't but if you had any
  • 01:48:30 close events this would be the point of
  • 01:48:32 time where no chairs execute stare
  • 01:48:34 appropriate callbacks
  • 01:48:36 so roughly spoken we have timer
  • 01:48:39 callbacks we then have any i/o related
  • 01:48:41 callbacks and other event callbacks and
  • 01:48:44 set immediate followed by close event
  • 01:48:47 called X so close events are basically
  • 01:48:49 handled
  • 01:48:50 separately or their callbacks are
  • 01:48:52 handled separately we should say well
  • 01:48:55 and then we might exit the whole node.js
  • 01:48:59 program but only if there are no
  • 01:49:02 remaining event handlers which are
  • 01:49:06 registered and that is what I mean with
  • 01:49:08 this Refs
  • 01:49:09 equal null thing here internally note J
  • 01:49:12 s keeps track of its open event
  • 01:49:15 listeners and it basically has a counter
  • 01:49:17 references or refs which it increments
  • 01:49:20 by 1 for every new callback to this
  • 01:49:24 register every new event listener that
  • 01:49:26 is registered so every new future work
  • 01:49:29 it has to do you could say and it
  • 01:49:31 reduces that counter by 1 for every
  • 01:49:34 event listener that it doesn't need
  • 01:49:35 anymore
  • 01:49:36 every callback it finished and since in
  • 01:49:39 a server environment we create a server
  • 01:49:43 with create server and then listen to
  • 01:49:46 incoming requests with listen this is an
  • 01:49:48 event which never is finished by default
  • 01:49:51 and therefore we always have at least
  • 01:49:53 one reference and therefore we don't
  • 01:49:55 exit in a normal node web server program
  • 01:49:58 we can call the exit function as you
  • 01:50:01 already saw and if we do anything else
  • 01:50:04 like at the beginning of the course when
  • 01:50:06 we just use node to execute a file that
  • 01:50:09 did not listen to a web server or on a
  • 01:50:12 web server then it all the finishes
  • 01:50:14 eventually once it's done with its work
  • 01:50:17 so this is how the event loop works and
  • 01:50:20 this can be very theoretical all these
  • 01:50:24 different phases typically don't matter
  • 01:50:27 to you certainly not when getting
  • 01:50:29 started with no chairs now consult these
  • 01:50:32 advanced resources if you want to learn
  • 01:50:34 more for now just be aware of that first
  • 01:50:37 picture I showed you of how nodejs
  • 01:50:40 handles complex work that there is this
  • 01:50:43 worker pool that takes care that your
  • 01:50:45 things are not getting plucked and that
  • 01:50:48 it therefore stays performant now I also
  • 01:50:51 touched on that security thing with that
  • 01:50:54 one javascript fret and regarding that
  • 01:50:58 we also got no problems by default
  • 01:51:00 though I will come back later to
  • 01:51:03 how we can manage global data and how we
  • 01:51:07 should manage it to ensure that we do
  • 01:51:09 not spoil data from request a to request
  • 01:51:12 B by default we have some separation
  • 01:51:14 because remember that callback method in
  • 01:51:17 create server it's executed for every
  • 01:51:20 new incoming request and therefore this
  • 01:51:22 function only runs for that incoming
  • 01:51:24 request and anything we do to the
  • 01:51:26 request or a response object there will
  • 01:51:28 not be exposed to have a request or
  • 01:51:30 response objects because each function
  • 01:51:32 is only scoped to itself and not
  • 01:51:34 accessible by the average so by default
  • 01:51:37 we have that separation due to how
  • 01:51:39 JavaScript works now that was a big
  • 01:51:43 chunk of theory again not something you
  • 01:51:46 have to learn by heart but something
  • 01:51:48 where you should at least keep that big
  • 01:51:49 picture in mind of how nodejs
  • 01:51:52 manages its work
  • 01:51:57 so let's wrap this module up and right
  • 01:52:00 before we finish let's actually start
  • 01:52:02 wrapping it up by improving our code a
  • 01:52:04 little bit we got all this code in this
  • 01:52:07 file and actually typically you write
  • 01:52:10 models or work with multiple files and
  • 01:52:13 why don't we create a new file that
  • 01:52:16 actually contains our routing logic so
  • 01:52:20 the logic where we check the URL and do
  • 01:52:22 different things so I'll create a new
  • 01:52:24 file here routes dot J s the name is up
  • 01:52:28 to you and I get this special item by my
  • 01:52:30 IDE it is an old JavaScript file don't
  • 01:52:33 be confused and in that file I basically
  • 01:52:37 want to have my if statements here and
  • 01:52:39 my default response code so I'll cut all
  • 01:52:43 of that out of here so that this is a
  • 01:52:45 pretty lean file and move it into routes
  • 01:52:48 chess now this wouldn't work like that
  • 01:52:50 let me say that we'll have to tweak that
  • 01:52:52 but this is now the code moved over I
  • 01:52:54 don't need the file system in Apps is an
  • 01:52:58 important remove that import
  • 01:53:00 we do need HTTP because we still use
  • 01:53:02 that there and we don't need the URL and
  • 01:53:05 method here so let's remove that go over
  • 01:53:08 to routes chess add this FS import it
  • 01:53:11 and hop and now start working with that
  • 01:53:14 now what do we need to do in this file
  • 01:53:16 we somehow need to be able to connect
  • 01:53:20 app js2 routes chess right because we
  • 01:53:22 need to be able to send our incoming
  • 01:53:25 request queue that file so to say and
  • 01:53:28 for that let's create a new function
  • 01:53:32 I'll name it request handler and we can
  • 01:53:38 either create a function like this it
  • 01:53:40 should receive request and response as
  • 01:53:43 arguments just as this function does
  • 01:53:46 because will effectively replace that
  • 01:53:48 function or we use a es6 function
  • 01:53:51 storing it in a constant request handler
  • 01:53:55 which looks like this bit of a strange
  • 01:53:57 syntax if you've never seen it before
  • 01:53:59 but we're essentially creating an
  • 01:54:01 anonymous arrow function which we store
  • 01:54:03 in a constant and this therefore is the
  • 01:54:05 function name now here we again receive
  • 01:54:08 requests and response and in
  • 01:54:11 that function we now move all that code
  • 01:54:14 because that code obviously uses the
  • 01:54:15 request and response objects so we need
  • 01:54:18 to have them available as local
  • 01:54:20 variables and we do now because these
  • 01:54:22 arguments are now named this way we also
  • 01:54:25 use URL and method so we need to re add
  • 01:54:28 these constants by getting that data
  • 01:54:31 from the request request URL and request
  • 01:54:35 method and now we just need to export
  • 01:54:39 this handler we're importing with this
  • 01:54:42 required syntax but how are we exported
  • 01:54:44 in no chess there are two ways of
  • 01:54:47 exporting the first one is to go at the
  • 01:54:51 bottom and add module dot exports this
  • 01:54:54 is another keyword or object which is
  • 01:54:58 exposed globally to you by nodejs which
  • 01:55:01 has a exports property and we can assign
  • 01:55:04 a value to this like our request handler
  • 01:55:08 so this constant which holds this
  • 01:55:10 function it's now stored in module
  • 01:55:13 exports and since this is a global
  • 01:55:15 object exposed by node note is actually
  • 01:55:18 able to work with this and we can now
  • 01:55:21 import from that routes JS file by
  • 01:55:24 requiring it and node will look for
  • 01:55:27 module exports and see if something was
  • 01:55:30 registered for this file here and we do
  • 01:55:34 register something in module exports the
  • 01:55:36 request Handler and you can register
  • 01:55:38 anything here you can add a new
  • 01:55:40 JavaScript object with multiple key
  • 01:55:42 value pairs whatever you need here I'll
  • 01:55:44 just register my function so now I can
  • 01:55:48 get go back to App j/s and import my
  • 01:55:51 routes the name of that constant is up
  • 01:55:53 to you by requiring it and since this is
  • 01:55:57 now not a global module we don't just
  • 01:56:01 type routes instead we want to add a
  • 01:56:04 local path to it with dot slash and you
  • 01:56:08 can omit dot j s because no trace will
  • 01:56:11 automatically attach this at the end you
  • 01:56:14 can't add it though but I'll just say it
  • 01:56:16 type dot slash routes separated from the
  • 01:56:20 core modules to make it really clear
  • 01:56:21 that this is a custom file and now note
  • 01:56:25 go ahead and look for a routes J's file
  • 01:56:27 in the same folder as fjs which it will
  • 01:56:30 find and in that file it will look for
  • 01:56:33 module exports and see what's registered
  • 01:56:36 in there and now we export that request
  • 01:56:39 handler method and now we can use that
  • 01:56:42 it will be stored in that routes because
  • 01:56:44 we assign whatever is exported from that
  • 01:56:46 file in that routes constant so this
  • 01:56:49 routes constant will ultimately hold
  • 01:56:51 this function and now we can use that
  • 01:56:55 here as a handler routes don't execute
  • 01:56:59 it so no parenthesis just pass the name
  • 01:57:02 telling node hey please execute the
  • 01:57:05 function that's stored in routes for
  • 01:57:08 incoming requests and now if we save
  • 01:57:11 that and we restart the server and we
  • 01:57:14 reload this page this is looking good
  • 01:57:17 and this is also looking good we should
  • 01:57:20 have tests stored and message to text
  • 01:57:23 and we do so now we simply split our
  • 01:57:26 code over to files having one file which
  • 01:57:29 is very lean that's just spins up the
  • 01:57:31 server but and that's important that
  • 01:57:34 also creates a connection to another
  • 01:57:35 file through that import and through
  • 01:57:39 that export where we export our request
  • 01:57:43 handler function here this is how that
  • 01:57:46 works now one important note about nodes
  • 01:57:49 module system the file content here is
  • 01:57:52 actually cached by node and we can't
  • 01:57:55 edit it externally so if we somehow
  • 01:57:57 would define routes as an object and we
  • 01:58:00 try to add a new property on the fly
  • 01:58:02 here this would not manipulate the
  • 01:58:05 original file so this is basically
  • 01:58:07 locked not accessible from outside we
  • 01:58:10 can only export stuff that we can now
  • 01:58:12 read from outside though you could have
  • 01:58:15 functions which you export that start
  • 01:58:17 changing stuff inside of that file but
  • 01:58:20 let's not make the too complicated for
  • 01:58:22 now we'll see all of that throughout the
  • 01:58:24 course obviously for now we have that
  • 01:58:26 connection there's one average syntax
  • 01:58:29 you could use instead of module exports
  • 01:58:32 sometimes you export many things and you
  • 01:58:36 could do that by having an off
  • 01:58:38 checked which has like the handler key
  • 01:58:41 and that is my request handler function
  • 01:58:43 and then also it has the some text key
  • 01:58:46 which is some hard-coded text in this
  • 01:58:52 case now we would export few things
  • 01:58:55 and that is how you can group that or
  • 01:58:57 separate these two things and still have
  • 01:58:59 one export being managed here which is
  • 01:59:01 the most you can have and now in fjs
  • 01:59:04 routes would be that object and not that
  • 01:59:06 function so here we would have to access
  • 01:59:10 the handler property so this property
  • 01:59:14 which holds the function reference we
  • 01:59:16 want to use and we could also simply
  • 01:59:19 output console dot log routes subtext
  • 01:59:24 here so this is how we can have multiple
  • 01:59:26 exports in one file now you see some
  • 01:59:29 hard-coded text here from this console
  • 01:59:31 lock and we still have the same
  • 01:59:34 functionality as before now there all is
  • 01:59:37 a different way of exporting multiple
  • 01:59:39 things besides this code which you can
  • 01:59:41 of course use you can also have module
  • 01:59:44 that exports dot handler request Handler
  • 01:59:48 and then all the module that exports dot
  • 01:59:52 some text some hard-coded text now it
  • 01:59:58 might look different but we still only
  • 02:00:00 have one export we still only have
  • 02:00:02 module exports which bundles all the
  • 02:00:04 exports but we explicitly assign the
  • 02:00:07 different properties like this so this
  • 02:00:09 is basically equivalent to disk code if
  • 02:00:12 I now save this and restart my server
  • 02:00:14 oops we see some hard-coded text and if
  • 02:00:19 I submit this it also still works last
  • 02:00:22 but not least there is a shortcut for
  • 02:00:24 this syntax you can now emit module and
  • 02:00:27 just write exports this is simply a
  • 02:00:29 shortcut supported by nodejs not some
  • 02:00:32 general JavaScript magic it's just an
  • 02:00:34 explicit shortcut supported by nodejs
  • 02:00:37 where now we also have multiple exports
  • 02:00:41 being merged together into one export so
  • 02:00:44 therefore again when I execute this we
  • 02:00:46 see some hard-coded text and some last
  • 02:00:50 value being submitted here
  • 02:00:53 still works and still ends up in message
  • 02:00:57 text so this is how we can connect
  • 02:01:00 multiple files by exporting either one
  • 02:01:04 element one function as we had it
  • 02:01:06 initially with module dot exports equals
  • 02:01:10 request handler right this is what we
  • 02:01:12 had initially let me comment this out
  • 02:01:14 with two slashes in front of it or
  • 02:01:17 module exports pointing at an object to
  • 02:01:20 combine multiple things or using module
  • 02:01:26 dot exports dot handler equals request
  • 02:01:29 handler and module dot exports dot some
  • 02:01:34 text equal some text or again this
  • 02:01:42 shortcut which is the same as this just
  • 02:01:44 with this shortcut offered by nodejs
  • 02:01:47 so this is how imports and exports work
  • 02:01:49 how the module system works and will
  • 02:01:51 work with a lot of files throughout the
  • 02:01:53 course so this is important to
  • 02:01:54 understand with that out of the way
  • 02:01:57 let's now finally wrap this module up
  • 02:02:03 so let's sum this module up now first of
  • 02:02:07 all we had a brief refresher on how the
  • 02:02:09 web works and in general it looks like
  • 02:02:11 this client so the browser sends a
  • 02:02:14 request to the server the server that's
  • 02:02:16 a magic reaches out to a database works
  • 02:02:19 with files sends back a response can be
  • 02:02:21 HTML can be something different and
  • 02:02:23 sends it back to the client the browser
  • 02:02:25 which then can displayed it and that is
  • 02:02:27 the entire flow we also saw in this
  • 02:02:29 module now note J ass is the part that
  • 02:02:32 runs on the server and regarding its
  • 02:02:35 lifecycle there is one important concept
  • 02:02:38 and that is that so-called event loop
  • 02:02:41 nodejs code runs in a non-blocking way
  • 02:02:44 which means we only register a bunch of
  • 02:02:46 callbacks and events and nodejs will
  • 02:02:49 eventually trigger that code once a
  • 02:02:52 certain task is done so that the
  • 02:02:54 javascript fret is always free to handle
  • 02:02:57 new events new incoming requests and
  • 02:03:00 stuff like that a node program can exit
  • 02:03:03 if there is no more work to do but on a
  • 02:03:06 server this wall should at least never
  • 02:03:08 happen because create server registers
  • 02:03:11 an event listener for an event which is
  • 02:03:13 never done if a new event is triggered
  • 02:03:15 so when you request is received this
  • 02:03:18 does not mean that node n registers the
  • 02:03:21 event listener instead we keep on
  • 02:03:23 listening for more requests and that is
  • 02:03:25 of course how a server should behave so
  • 02:03:27 it's this cycle that's important to
  • 02:03:29 understand and that we have this loop
  • 02:03:31 which always keeps on going keeps on
  • 02:03:33 waiting for new events and which does
  • 02:03:35 something when some event happens and
  • 02:03:38 then basically dispatches some actions
  • 02:03:40 to you the operating system you could
  • 02:03:43 say for example and then again frees up
  • 02:03:46 the thread now this all involves a lot
  • 02:03:50 of asynchronous code which we saw with
  • 02:03:52 all the callbacks the JavaScript code
  • 02:03:55 should be non-blocking so we have this
  • 02:03:57 callback an event-driven approach where
  • 02:04:00 we are able to register some code to be
  • 02:04:03 executed in the future instead of
  • 02:04:05 running right away and blocking the main
  • 02:04:08 threat because this has to be avoided
  • 02:04:10 under all circumstances and Noches
  • 02:04:13 is built around that concept of avoiding
  • 02:04:16 this issue
  • 02:04:17 we also saw how to work with requests
  • 02:04:20 and responses denote J s way we saw that
  • 02:04:23 we have to parse the request data which
  • 02:04:26 arrives in chunks and that we can use
  • 02:04:29 this concept of streams and buffers
  • 02:04:32 which I explained and that we should
  • 02:04:34 avoid sending double responses so that
  • 02:04:37 after rest end you must not send another
  • 02:04:41 response and this can help me easily if
  • 02:04:43 you forget about that asynchronous
  • 02:04:46 nature and that a line of code you write
  • 02:04:48 might not execute immediately so
  • 02:04:51 depending on where you write it if it's
  • 02:04:53 an event listener it will not execute
  • 02:04:55 immediately that is what I mean and that
  • 02:04:57 is what is important to keep in mind
  • 02:04:59 here we also learned that note J s is
  • 02:05:03 all about using its built-in
  • 02:05:05 functionalities and whilst there are
  • 02:05:07 some global variables or objects or
  • 02:05:11 functions we can use this also means
  • 02:05:13 that we should use its core modules core
  • 02:05:16 modules are things like the HTTP DFS or
  • 02:05:19 the path module there are more and you
  • 02:05:21 can learn all about them in the official
  • 02:05:24 noches
  • 02:05:24 Doc's of course will also use quite a
  • 02:05:27 lot of them throughout this course and
  • 02:05:28 these core modules give us a couple of
  • 02:05:31 different functionalities that allow us
  • 02:05:33 to basically do whatever we ever could
  • 02:05:36 want to do on a server like create a new
  • 02:05:40 server with the help of the HTTP module
  • 02:05:42 they're imported why adi require syntax
  • 02:05:46 and we can only use them in the file
  • 02:05:49 into which we import them and if you
  • 02:05:50 want to use them in two different files
  • 02:05:52 we have to import them in both files
  • 02:05:54 separately now that leads us to the node
  • 02:05:57 module system and this basically works
  • 02:06:00 with the help of this require keyword
  • 02:06:02 which pulls some functionality from one
  • 02:06:06 of our files if we start with slash or
  • 02:06:08 dot slash or from a core or third-party
  • 02:06:12 module we haven't used any third-party
  • 02:06:15 modules thus far but we'll all do that
  • 02:06:17 soon and it basically pulls in whatever
  • 02:06:20 we export there and stores it in a new
  • 02:06:22 variable or constant as we did it in
  • 02:06:25 this module and export is an important
  • 02:06:28 keyword here we do export with the
  • 02:06:31 of module dot exports or for multiple
  • 02:06:34 exports with the exports shortcut I
  • 02:06:37 showed you in the last lectures so this
  • 02:06:40 is what we learn in this module and I
  • 02:06:42 know that this was a lot of theory or
  • 02:06:45 nitty-gritty stuff about no chess
  • 02:06:48 doesn't look too easy and beautiful but
  • 02:06:50 it'll get way more beautiful from now on
  • 02:06:52 it is super important to never forget
  • 02:06:55 what no chess isn't dust for you though
  • 02:06:58 because many courses right away start
  • 02:07:00 with expressed J s which will all use
  • 02:07:03 starting soon and therefore you never
  • 02:07:06 really learn what's happening under the
  • 02:07:08 hood which is sad because this is
  • 02:07:09 important and makes you a better note
  • 02:07:12 developer that let's move on