Lesson 13

Using Node, Express, and SuperLogin

Creating a server to handle requests from the application

PRO

Lesson Outline

Using Node, Express, and SuperLogin

We are going to need to set up a simple server in order to handle registration and authentication in our application. To begin with, we will just run this server locally like we are doing with the CouchDB database, but eventually, we will host it somewhere so that it can be accessed remotely (not just from your computer).

We are going to be creating a simple Node/Express server for the application. There will be very little code required to get the server set up, and we are also able to use a package called SuperLogin that is going to make our lives a lot easier. SuperLogin will basically handle everything we need to do for creating and authenticating users with CouchDB. We will need to perform just a little bit of configuration for SuperLogin on our server, and then it will expose a bunch of different routes that we can use to perform various actions (like login, logout, register, and so on).

In my opinion, it's generally a good idea to let someone else handle the authentication portion of your application. Authentication/authorisation is complicated and there are a lot of potential security concerns, so unless you really know what you are doing I don't think it's a good idea to create your own authentication system from scratch. Cloud services are generally the best in this regard as you don't really touch any of the authentication stuff. A library like SuperLogin handles a lot of the important security implementation details for us as well, but we are still handling some of the implementation details ourselves so we need to be careful.

We will be using a little bit of NodeJS code to create this server, but I don't intend to teach the basics of Node in this module. It will certainly be useful to have a basic understanding of Node, but you will be able to progress through these steps just fine even if you don't.

Creating the Server

In order to set up the server, you will need to create a new folder outside of your existing Ionic project. You can create this folder wherever you like but typically people will create a project with two folders: a client folder to contain the front end (the Ionic project) and a server folder to contain the server.

Create a server folder outside of your Ionic project to contain your server

Run npm init inside of the server folder to initialise a Node project (you can just leave most of the fields blank)

Run the following command to install all of the dependencies:

npm install cors del dotenv express https method-override morgan @sl-nx/superlogin-next @sl-nx/couch-pwd

To make sure that things remain steady when deployed, we are going to specify a Node version in the package.json file as well as adding a start script. Your final package.json should look something like this:

PRO

Thanks for checking out the preview of this lesson!

The full version of this lesson is only available to pro members. If you would like full access to this module and all of the other pro modules on Elite Ionic you can become a pro member (or log in if you are already a member).