See Coding with Node.js: Part 1; Getting started with Express. for the beginning of this tutorial series.
Now that we have a working website model with error pages and some useful content the next step is to connect to a database so that we can later administer the content of the website and store it in the database.
For this tutorial we are going to setup a simple login system into mongo with the help of mongoose. In the next tutorial add in the blog posts to the database.
First thing we need to do is add mongoose to the project. This is done by editing the package.json file and adding the dependancy:
Next what we need is MongoDB installed on your system, you can visit http://www.mongodb.org/ for instructions on getting Mongo setup.
Mongo is a nosql database and also does not have a set design for the tables (or collections as they are called in mongo) so you will need to define how you wish for your data to look programmatically. (this also allows for quick and easy changes to the structure later) For defining the structure and to setup all the needed models for the database we will create a database object.
This will allow us to connect to the database then create all the needed schemas and models for the project on load. As well as retrieve the models later for use with retrieving rows/documents and saving the same.
Now that we have a db object we can add this to app.js to have it connect to the database on load.
For adding users into the database I have chosen to do this via a script file so that adding the first user is easy and because this should be a rare task.
With the above file we first read the command line arguments then if the parameter count is sufficient we can then add a user to the database. It's as easy as that. Example:
$ node addUser.js firstname.lastname@example.org mypassword
With the database created, connected and a user added we're ready to setup some admin pages to allow us to login.
The above are pretty simple pages, I've setup the index to simply say you are logged in, and produced login page. They should be pretty self explanatory.
Now to display these pages we are going to require a little more logic then we previously used in the StaticPages.js file. Below I have setup a class function for each page to handle the individual pages.
Above most of the logic so far is in the pageLoginPost function and we determine the way the page will display depending on the status of the posted contents (the login and the password). In the _checkLogin function we check if a user is logged in and redirect them to the login page if they are not.
Finally to get these pages to work we will need to add to the express object support for sessions and cookies. Also we will add in support for the AdminPages object.
With all the above code in place you should now be able to access /Admin of the website and attempt to login to the website.
All code created for this website is available on the github page and this tutorial specifically is available here: https://github.com/mikevalstar/mikevalstar_com/tree/Node_Tutorial_pt3
In the next part of this series I will be adding in a blog post list and editor.