For my posts on express.js I want to try something different, have a post that acts as an index for all my content on express.js. This will serve as a central guide for all things with express.js, at least much of the must know stuff that one should be aware of. This post will also branch off into many other posts on express.js, and will likely grow over time as I keep adding, and updating content on express. Getting solid with express.js is not something that will happen over night, and it branches off into other subjects like database management, deployment, front end frameworks, and security. So this seems like it might be a good idea to help keep things more organized.
Like many other subjects on by github pages site here, this is a collection of posts on express.js. This post aims to be a main index of all content relevant to express on this site. If this works out well I may start to do this with all other categories on the site, as it seems like a good idea. Please note that this is also a relatively new collection as well, and as such this post will likely be updated a great deal as I continue working with express.
Yes express.js is a project where the version number matters a whole lot. As of this writing I am using express 4.16.3 with many of the posts that I have written so far, so assume that unless noted otherwise. If you run into issues with the content in my posts, aways remember to check the simplest things first like spelling mistakes, and yes the version number relative to what I am using in the content.
To get started with express.js you will need node.js installed, which should come with the package manager known as npm as well. There is other software of interest as well such as mongoDB, and having a recent web browser, but at a minimum you will need node.js installed.
Once node is installed, and hopefully npm as well. Getting started with any express.js project manually is just a matter of creating a new project folder, using npm to set up a new package.json file, and installing express.js with npm adding it to the package.json file with the save option.
All express projects will involve calling the main method that is given to create an instance of the app object. The app object will then contain methods that I can use to define what to do for certain http methods, and paths.
Here I am responding to get requests to the root path with the string hello word by making use of the send method in the response object.
Read the full post on the top level function, and static methods.
So the main express function can be used to create an instance of an app object when called. This app object contains useful methods that can be used to define paths, and get the app to start listening on a given port. However there are also some static methods attached to the function as well that can be used for things like setting up a static path.
No additional module is needed to set up a static server with express.js, it can quickly and easily be set up using the express.js method with app.use.
Where public is the name of a folder called public in the root name space of the express.js project. The express.static method can be used to quickly set up one or more static paths, in the event that you want to do something involving the use of a rendering engine for some paths, bust just simply serve static files with other paths.
Read more on routers here
For example I could have a file at \/routes\/main.js like this:
and then use it in my main app.js file like this:
Routers are like little express apps that can be used to help break down routing tasks like this.
There is a great deal more to know about the top level function that is exported. The main thing to know is that it returns a method that is called to create instances of an app object. However there are also some additional useful methods attached to it as well.
Read more on the app object in express
When calling the main top level express function that is exported when importing express into a project, and instance of app is returned. This is one of the most important objects when working with express, as it contains methods for setting paths, and handing http requests.
Click here to read more about app.use is is a must know.
The app.use method is your friend when starting to put something complex together. As a project grows there will start to be many middeware modules added into the mix, there will also be a desire to break the code of your project into smaller components that will then be used with the main app.js file. This is where the app.use method comes into play.
be sure to read more on the get method.
The first app object method that most people will use when starting with express.js might be app.get. This is a method that is used to define how to work with incoming http GET requests for a given path or pattern. In addition to working with get requests, this method can also be used to get app settings as well.
The order in which I call app.get is important, as the first use of the method is what will be used first for all incoming get requests. The next use of app.get will only fire for the root path, and the last one will fire for all get request that are not satisfied above.
Read more on app.all
So where that methods that I give to app.get will only fire for GET requests, app.all will fire for any kind of request that is received.
When using app.all the method property of the request object is of interest, as it will tell me what kind of method has been used.
For more on using req.app, or res.app to get at an app object read this.
When I start to make a project that is a little complicated, I end up in a situation in which I am dealing with many instances of an express app. Other times I am interested in grabbing at my main app object without having to export it and then bring it into my other file with require.
Thankfully in both the request, and response objects there is a reference to the app that is using the middeware. Sometimes that can be used as a way to use whatever it is that I need to use from the main app object without having to make a new instance of app.
read my full post on request objects.
The request object is an object that contains all kinds of useful information about an incoming http request.
Read more on request headers in express.js
When working with requests it is often desired to look at the incoming request headers. For exmaple if I want to know the user agent string. For this there is the req.headers array that will contain all the headers that are present in the request, and the req.get method that can be used to get a certain header from that array.
As the name suggests the response object contains methods and properties relevant to the act of responding to an incoming http request. Here I will be taking a moment to cover relevant topics on the response object, and some of the most important aspects to know about.
For more on using ejs with express.js I have a post on that.
The render method is for rendering a view set via app.set. There are many rendering engines to choose from, but so far I have been sticking to ejs.
I have a full post on the sendFile method here if you want to know more.
If for some reason I want to send just a single file as a response I can do that with the send file method. This will work in a similar fashion as setting up a static file path, but can be used to just serve up a single file, and not a whole folder.
This will serve up a file called face.png in a folder called img at the root of a project. I need to use the root option if I want to use relative paths like this.
To find out more about this I have a post on the response download method.
If I want to have a path that will result in a file being download to the client that can be done very easily with the download method.
There are many modules for express.js that are closely tied to express.js, some of which come with express such as body-parser, Others need to be installed with npm first. In this section I will be briefly covering some of the must know modules for express.
Read more about body parser here, there is much to know about it.
The body parser module comes in handy whenever I want to do something involving post requests. This module makes the process of parsing an incoming payload into something I can work with a snap. There are methods for parsing the usual json data, as well as others for plain text, and even binary data as well.
Read more on cookie parser here.
The cookie-parser middeware is an offical express.js middeware that is used to parse a cookie header, and populate req.cookies.
Check out my full post on express session
If you are looking into how to get started with session management with express there is of course express-session, it is a decent solution to help with session management.
User authentication, or in other words loggin in, is a complicated subject. This is where things get a little hairy for most people, but luckily there are great solutions to help simplify this process. If you are thinking about coming up with your own solution for this, don’t, unless you can really think of some way to rationalize it.
Read my post on getting started with passport
So passport may be the best known, and also best supported solution for authentication with express.js. There are many strategies for authorization with passport, including simple local solutions, as well as others that involve oAuth.
check out my getting started with micro servies post.
If you are thinking about the creation of an app being this thing where everything works as just one big chunk of code that is deployed to just a single node at a hosting company and thats all there is to it, then maybe you should start looking into micro services. This is a way of breaking things down into not just one app, but a whole bunch of apps that all work together. This helps make things more manageable, and it can also reduce the work load of an app, by making some things the responsibility of a whole different app, on a whole different node, that may even be at a whole separate hosting company.
I do not have much content on this for the moment, so far I have just started playing with something called helmet. If I get to it I should make one or more projects that help show some do, and do nots for this.
Read more on dns prefect control with helmet.
The Subject of DNS prefetching is something that can be considered a bit of a privacy concern. The thing about it is that it seems many browsers do it by default, for the purpose of speeding things up a bit. However if someone where to observe my traffic with something like wrireshark it would look like I am going to all kinds of sites, but in reality there are just links to those sites on the page.
the express middeware helmet can be used to set headers that keep this from happening as well as do a whole bunch of other things that might help improve security, and provacy for an express.js app.
I wanted to try something new when writing my content on express. I think having a main post like this that links to everything else on express is very useful. If This post does well, I might start having posts like this for all of my content categories.
If I do get to it, I will eventually build one or more real projects using express. This will mean more posts on security, scaling, and deployment, as well as improved quality, and quantity of posts in general.
I hope this post has done a decent job of giving a general overview of express.js, if not from here you should be able to navigate to more specific posts on various topics on express.