AngularJS Framework : Token (JSON Web Token - JWT) based auth backend with NodeJS
This tutorial is the backend part of token based authentication.
The code is available here:
Token-Based-Authentication/Backend-Auth-NodeJS.
We may want to clone it:
$ git clone https://github.com/epic-math/Token-Based-Authentication.git
In the back-end project, there are three main files:
- package.json is for dependency management.
- models\User.js contains a User model that will be used for making database operations about users.
- server.js is for project bootstrapping and request handling.
We need to define an environment variable: MONGO_URL which is used for the database connection URL:
export MONGO_URL="mongodb://localhost/"
Once clone the source, setup the MONGO_URL, we can install the dependent packages:
$ npm install
This command reads Backend-Auth-NodeJS's package.json file and downloads the following tools into the node_modules directory:
We can start our server by using the following code snippet(client.js):
// Start Server app.listen(port, function () { console.log( "Express server listening on port " + port); });
Run the server:
$ node client.js Express server listening on port 3000 GET / 200 149.680 ms - 2704 GET /lib/ngStorage.js 200 13.399 ms - 1179 GET /scripts/app.js 200 17.246 ms - 1489 GET /scripts/controllers.js 200 14.306 ms - 1789 GET /scripts/services.js 200 1.924 ms - 1811 GET /partials/home.html 200 4.793 ms - 236
Our browser:
We finished the back-end service, and it can be used by multiple clients.
More detailed explanation about the cloned NodeJS codes will be added later.
Now, we want to move into the front-end project, and in next chapter (Token (JSON Web Token - JWT) based auth frontend with AngularJS), we will see an AngularJS project for token-based auth.
Token-Based Authentication With AngularJS & NodeJS
AngularJS
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization