NodeJs with Redis Web App Part 1

Introduction

This is part one of a tutorial series that will explain how to build a simple NodeJs Redis web app. The app can then be used for managing user information with NodeJs and Redis. As Redis is an efficient key-value data store, it supports a number of structures. Because is provides such great flexibility, Redis will help increase the speed of applications, including applications built in Node.js.

Prerequisites

  • NodeJs must be properly installed and configured
  • Nodemon must be installed globally. Use the following command to install Nodemon: npm install -g nodemon
  • Redis must be properly installed and configured with the server running in the background. The installation procedure for Redis can be found on the official Redis website here: Redis.io
  • Possess a basic knowledge of JavaScript
  • A text editor of choice. Note that this tutorial will use VSCODIUM in the examples.

Configuring the Project Resources via Node Package Manager

This section will explaing how to configure project resources using the NodeJs package manager.

Creating the Basic Package.json File

  • Create a folder to host the project and give the folder a meaningful name.

  • Navigate to that directory via the terminal and generate the basic package.json using the following command:

npm init

The system will then request details. The output should resemble the following:

{
"name": "usermanagement",
"version": "1.0.0",
"description": "Basic user management app",
"main": "app.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "Rommel Galisanao",
"license": "ISC"
}

Installing Dependencies

With the package.json file now created, install the dependencies the application requires with the following command:

npm install body-parser express express-handlebars method-override redis --save
  • body-parser – This command allows parsing of the incoming request made by the client via form element, such as the form’s input.

  • express – This is a framework command for NodeJs.

  • express-handlebars – This commad is a view engine that allows for the creation of a frontend design.

  • method-override – This command permits performing an override to the request method property. This command will be discussed later in greater detail, with examples.

  • redis – This command allows users to interact with the Redis server.

Creating the Entry Point of the Web Application

With the needed information stored in the package.json file, configure the main entry point of the application.

Execute the following code and create a file named app.js in the root directory of the project:

// The following block of code requires all the dependencies that allow for access methods for the application:
const express = require('express');
const expressHan = require('express-handlebars');
const path = require('path');
const bodyParser = require('body-parser');
const methodoverride = require('method-override');
const redis = require('redis');

// This block of code creates a Redis client and provides notification via the console if a successfully connection to the Redis server was established:
let rclient = redis.createClient();
rclient.on('connect', function(){
console.log('connected to redis server');
});

// This command sets the port and instructs the NodeJs server where to listen:
const port = 3000;


// This command initializes an instance of express and assigns it to an object:

`app` const app = express();

// This creates a view templating engine that allows creation of the web application pages:
app.engine('handlebars',expressHan({defaultLayout:'main'}));
app.set('view engine','handlebars');


// This body-parser creates middleware for parsing json and urlencoded data:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

// This Command creates the method override:
app.use(methodoverride('_method'));

// This command creates a route for a Search Page:
app.get('/',function(request, response){
response.render('searchuser');
});

// This code creates a route to process the RETRIEVE operation:
app.post('/user/search', function(request,response,next){
let id = request.body.id;
});


// This command instructs the Node server to listen to port 3000 and send a notification, via the console, when the server has been successfully started:

app.listen(port, function(){
console.log('Server started and listening on port '+port);
});

Now run the following command in the terminal to confirm the configuration is working properly:

nodemon app.js

The output should resemble the following:

[nodemon] 1.19.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
Server started and listening on port 3000
connected to redis server

Note: Ensure the Redis server is running in the background.

Conclusion

This was part one in the tutorial series explaining how to build a simple NodeJs Redis web app. The tutorial proved explanations with coding examples. The article specifically explained how to configuring the project resources via the node package manager, how to create the basic package.json file, install the needed dependencies, create the entry point of the web application and how to confirm the configuration is working properly. It is important to remember that the Redis server must be running in the background in order to execute these operations.

The Code

Following is the code used in this tutorial in its entirety:

The app.js

const express = require('express');
const expressHan = require('express-handlebars');
const path = require('path');
const bodyParser = require('body-parser');
const methodoverride = require('method-override');
const redis = require('redis');

let rclient = redis.createClient();
rclient.on('connect', function(){
console.log('connected to redis server');
});

const port = 3000;


const app = express();

app.engine('handlebars',expressHan({defaultLayout:'main'}));
app.set('view engine','handlebars');


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

app.use(methodoverride('_method'));


app.get('/',function(request, response){
response.render('searchuser');
});

app.post('/user/search', function(request,response,next){
let id = request.body.id;
});

app.listen(port, function(){
console.log('Server started and listening on port '+port);
});

The package.json

{
"name": "usermanagement",
"version": "1.0.0",
"description": "Basic user management app",
"main": "app.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "Rommel Galisanao",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-handlebars": "^3.1.0",
"method-override": "^3.0.0",
"redis": "^2.8.0"
}
}

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.