Watching:  

Getting started with ExpressJS


Author: Subject Coach
Added on: 5th Mar 2015

 
Please note: You need to login to view this resource

This course is for beginners who wants to get started with ExpressJS framework for NodeJS. This course is subdivided into 13 parts as listed below

  1. Welcome note
  2. Installing NodeJS and ExpressJS
  3. Developing your first application with ExpressJS
  4. Working with Views/Templates
  5. Handling requests
  6. Application specific settings
  7. Midlleware and request flow
  8. Understanding routes
  9. Modularizing routes in ExpressJS
  10. Request Object
  11. Response Object
  12. A word on locals
  13. Conclusion

Please let us know how we can do better next time by leaving your feedback. 

 

Author: Subject Coach
Added on: 5th Mar 2015

Please get in touch with your teacher or tutor in case you have a question related to this lesson

None just yet!

In the last chapter we learned how to install node and expressJS. In this chapter we will be writing our first express application.

On the right-hand side I have a terminal session opened with my remote machine and on my left I have my editor opened.

First thing I will do is to launch NPM tool to initialize and create our application. Let's do that now, first thing it's asking me to provide the name of the application, I will just press enter and then it's asking me about version number for my application, I am happy with the default value, I will press enter here. If I want to provide description for my application I can do that now, but I can also do that later, for the time being I will just press enter. The next thing will be the entry point script, and I will change that to app.JS. I don't have any test command for this application, So! I can safely press enter again. Next is to provide any git repository information, and I don't have any. You can provide keywords for your application here for future reference, but it's not necessary, so just press enter. I will provide the author name here and license I don't really care at this stage, so I will press enter. I can give my confirmation next the above information is correct by typing yes, and when I press enter now you will notice that there is a package file created. This file can be updated anytime.


One more thing to do is to install expressJS under this folder. So let’s do that by typing,

NPM install express.

Once express is installed for this project, you will see all the relevant files under node modules folder, these are the files that express depends on, in order to do its thing.

Let’s start working on app.JS file. To use a module will use a node method called require. Express can be initialized as,

var express = require(‘express’);

Next I will initialize an app variable. This variable takes the return value of express function. Now all the functions and properties of express object can be accessed through this app variable.

Next we will create a route for HTTP GET request for home page for our website. The get method also takes second parameter as a callback function that takes request and response parameters, request and response parameters are exactly the same as provided by node. We can use response object's send function to send a response back to the browser. Let’s see where you can find information on such functions with express API reference. Go to expressJS.com, and browse to the API reference. Choose the version of express that you are using, and then find the response object reference. Under this object you will find a send function. Here you can see what send function can do for you with various examples.

Let’s now go back to our app.JS file. Now I will declare another variable called "appserver", which basically holds the return from app.listen method. We will listen for incoming connections on port 3001. Listen function of express object takes second parameter as a callback function, we can output some debug information such as printing a string such as, "listening on port 3001" or we can also output some other information such as which port is being used and stuff like that.

I will now go back to my terminal and launch my application with issuing this command, console.log will print the information I was seeking on successful port binding and successful listen operation.

Now if I go to my browser and type my IP address followed by port number 3001 you will see that "hell and welcome to expressJS" gets printed. And of course I meant hello, rather than hell in that string, I can go back and correct this and also add some HTML tags. After making this change, I will quickly relaunch my application, now! when I go back to the browser and refresh my page, I will see the updates.

So you see it’s not that difficult to write HTML content using express and node. But let's take our express knowledge to another level with chapters to follow.

In the next chapter I will talk about views and templates.