Coding

Node.js – Tutorial – First Application

  • 00:00:01 hey everyone happy to have you back in
  • 00:00:03 this video we're going to install notes
  • 00:00:05 yes and we are also writing our very
  • 00:00:08 first application let's get started to
  • 00:00:11 install no chairs we have to head over
  • 00:00:14 to noches org and there you will find a
  • 00:00:17 link to the latest mature and dependable
  • 00:00:20 binary for your system simply download
  • 00:00:22 it extract it and then run through the
  • 00:00:24 installer that's pretty straightforward
  • 00:00:26 once installation is finished you can
  • 00:00:29 check that everything was successful by
  • 00:00:30 running node – V in the terminal or
  • 00:00:33 command line and then you should
  • 00:00:34 calibration number indicating that well
  • 00:00:36 now it is available now that we have
  • 00:00:39 node we can jump right into writing our
  • 00:00:41 first application because the cool thing
  • 00:00:43 about nodejs is we don't have to install
  • 00:00:46 any separate server or something like
  • 00:00:48 this we're going to write our server on
  • 00:00:51 our own and it's very easy to do so as
  • 00:00:53 you'll see so I got phpstorm which is my
  • 00:00:57 ID ear and my personal opinion is that
  • 00:01:01 phpstorm and webstorm are great ideas
  • 00:01:04 for working with well JavaScript
  • 00:01:06 webstorm or if you want a complete
  • 00:01:08 package including PHP PHP store now you
  • 00:01:11 don't have to buy them they cost money
  • 00:01:13 and I'm sure Adam or sublime text or
  • 00:01:16 great editors – but for the rest of well
  • 00:01:19 this channel probably I'll mostly work
  • 00:01:23 with phpstorm and if you're interested
  • 00:01:25 in following along as close as you can
  • 00:01:27 you might check out this great ide but
  • 00:01:30 that's just my opinion so what I'll do
  • 00:01:33 is I'll create a new project and I could
  • 00:01:36 select Noches
  • 00:01:37 Express up here but we'll create a pure
  • 00:01:39 notes ASM without Express for the moment
  • 00:01:42 we'll get into Express later in this
  • 00:01:44 series don't you worry so I'll just
  • 00:01:46 choose this empty project here and then
  • 00:01:49 I will select this nodejs path here I
  • 00:01:52 already set up and in this path here I
  • 00:01:55 want to create a new folder basics is
  • 00:01:58 just what I'll call this series here ok
  • 00:02:01 then I hit create and obviously you can
  • 00:02:04 create this folder in the finder or the
  • 00:02:05 windows explorer to basically just need
  • 00:02:08 to create a folder and I'll let me bring
  • 00:02:10 my my IDE over here
  • 00:02:15 so this is our newly created workspace
  • 00:02:18 and as you can see just an empty folder
  • 00:02:21 here and the first thing I'm going to do
  • 00:02:23 is I will create a new file and I will
  • 00:02:25 call it server dot J s because we're
  • 00:02:29 writing our first server we want to see
  • 00:02:32 something in the browser right from the
  • 00:02:34 beginning let me pause for a second it
  • 00:02:37 might be strange to someone coming from
  • 00:02:39 that say PHP that we now write our own
  • 00:02:42 server because there we would have
  • 00:02:44 Apache we would have our PHP code which
  • 00:02:46 gets interpreted and then our HTML files
  • 00:02:49 would get served to the user with our or
  • 00:02:52 via our server in this case here we're
  • 00:02:56 writing our own server how do we do then
  • 00:02:58 how does this work basically as I
  • 00:03:01 explained in the first video node.js is
  • 00:03:03 just a runtime executing our JavaScript
  • 00:03:05 files now note C as provide certain
  • 00:03:08 functionalities which allow us to well
  • 00:03:11 create a server like functionality or
  • 00:03:14 not only server like but a server
  • 00:03:15 functionality for example we can note
  • 00:03:18 yes that it should listen to a certain
  • 00:03:21 port on a certain domain and all
  • 00:03:24 requests coming in it should be handled
  • 00:03:26 in a certain way and this is exactly
  • 00:03:28 what we will specify in this server file
  • 00:03:32 we're creating right now there it will
  • 00:03:34 tell no chairs to listen to
  • 00:03:36 incoming requests and do something upon
  • 00:03:38 such requests so very very simple in the
  • 00:03:42 end you just have to kind of get into
  • 00:03:45 that thinking I guess let me show you
  • 00:03:47 what I mean so the server Duchess is the
  • 00:03:50 file which no chairs will execute and it
  • 00:03:53 will not only run through it once and
  • 00:03:55 then finish it but in this file we will
  • 00:03:57 kind of start a loop where no chairs
  • 00:04:01 will continue so you listen to requests
  • 00:04:03 to do this we first have to import or to
  • 00:04:06 require the HTTP module now modules are
  • 00:04:12 something you might know from my angular
  • 00:04:14 2 videos but they're we use typescript
  • 00:04:16 how does this fit in here this is
  • 00:04:19 JavaScript right in the next video I
  • 00:04:22 will take a step back and explain some
  • 00:04:25 of these basic Java
  • 00:04:26 script things we should know when
  • 00:04:28 working with no chairs marshals will be
  • 00:04:30 one of them for now just think of this
  • 00:04:33 like we're importing some functionality
  • 00:04:35 we need so I'm requiring this HTTP
  • 00:04:39 module which gives me access to the
  • 00:04:42 create server method here and this does
  • 00:04:46 exactly what it sounds like it creates a
  • 00:04:48 server now I'll chain another method
  • 00:04:51 here which is the listen method and this
  • 00:04:55 tells our server which we will create
  • 00:04:57 here to listen to the port 8000 so all
  • 00:05:02 requests coming in on port 8000 should
  • 00:05:05 be handled by the server now currently
  • 00:05:07 the server is doing absolutely nothing
  • 00:05:09 so in order to do something with these
  • 00:05:12 requests I will specify a method here
  • 00:05:15 the on request method which should
  • 00:05:18 handle these requests now this method
  • 00:05:20 doesn't exist yet so let's create it
  • 00:05:22 I'll create a new function here the on
  • 00:05:25 request function and this function takes
  • 00:05:29 two arguments the request and response
  • 00:05:35 these arguments are automatically passed
  • 00:05:38 into this function by nodejs when this
  • 00:05:42 function here is kind of executed or
  • 00:05:46 whatever request comes in in this on
  • 00:05:49 request method here I'm not doing
  • 00:05:52 anything with the requester but I went
  • 00:05:54 to well change the response I want you
  • 00:05:58 yeah
  • 00:05:59 tell noches which responds should be
  • 00:06:01 given so therefore I will call the right
  • 00:06:04 head method to set the the header of our
  • 00:06:08 response and this will get the status
  • 00:06:11 code of okay 200 and then I'll pass a
  • 00:06:15 JavaScript object as the second argument
  • 00:06:17 the reason as it says here which is
  • 00:06:21 content type and then text planes are
  • 00:06:27 here I'm just setting the content type
  • 00:06:28 of my response and then doing this as a
  • 00:06:31 JavaScript object where the key is
  • 00:06:34 well basically whatever I want to set in
  • 00:06:37 the
  • 00:06:37 and the value is one of the value of the
  • 00:06:41 key unsetting in the header so in this
  • 00:06:43 case which content type of course I
  • 00:06:47 could set our header keys there as well
  • 00:06:50 then I will call the right function to
  • 00:06:55 just render some plain text to the
  • 00:06:58 screen now of course we can also render
  • 00:07:00 HTML and we can use HTML files as
  • 00:07:03 templates and we reload and we will do
  • 00:07:05 all this in future videos for now I'm
  • 00:07:08 just outputting some text have a world
  • 00:07:11 of course the last thing I want to do is
  • 00:07:13 I want to call response and to make
  • 00:07:17 clear that I'm now done with handling
  • 00:07:20 response and that it can be output to
  • 00:07:23 well the personal sent requests so now
  • 00:07:27 our server is created and all that is
  • 00:07:29 now left to do is open up a terminal
  • 00:07:32 navigate into this folder where you
  • 00:07:34 saved a surrogate ES file and then just
  • 00:07:36 run node server JSO execute this Chase
  • 00:07:41 for Java Script file with no chairs hit
  • 00:07:45 enter and all you won't get any message
  • 00:07:47 but you can see it's not creating a new
  • 00:07:49 lines this process is still running and
  • 00:07:51 then in the browser I'll navigate to
  • 00:07:54 localhost 8080 ated and if there are
  • 00:08:05 still some things that are not clear
  • 00:08:07 100% don't worry we will go into all the
  • 00:08:11 details in the next videos and we will
  • 00:08:13 of course then create more awesome
  • 00:08:17 applications and so on see you the next
  • 00:08:19 videos bye