Why are we doing this?

This period of time in the development of a larger project, the background prophase preparation time is not long, the late development soon, early development will be faster, and the foreground of the late development of proportion also need time, in the presence of uncoordinated on schedule, the background of interface format has been given, although can mockjson return directly, but in many (such as add, delete, Modify) these need to respond to the operation is also very weak, need a simple background, can meet the needs of immediate development and debugging, specific requirements are as follows:

  1. Automation, don’t match an environment half a day
  2. In collaborative development, each database may be different, but the foundation must be the same
  3. High performance, do not compile ☕ are cold
  4. Affinity is good, the best is JS, as the front end to write up looks convenient

So under the screening of these conditions, I finally choose nodeJS + Redis scheme, start compilation is very fast, database migration backup and master and slave are very convenient

How does this work?

Project directory structure

| - mock_server// Simulate the home directory of the background| - node_modules// The main libraries are Express, Redis, body-parser, etc| - rest// It contains node routes, classified according to modules and conforming to restful specifications| -case.js | -... | - dump. RDB// Database backup for synchronous collaborative development| -- package. Json | -- changebase. Sh// When the database base changes| - setup. Sh// Install the Redis script. Ensure that the redis version is consistent and execute it only once| - startup. ShDump. RDB into the bin directory of redis to restore data| - startmock. Js// Node server entry function.Copy the code

The script content

setup.sh

#! /bin/bash

#Shell execution options:
#-n Reads shell scripts but does not execute them
#-x Displays each command executed in tracing mode
# -c "string"Read the command from strings

#Download directory
downloadsDir=/root/Downloads
#The installation directory
appDir=/usr/local/redis

#Check whether the backup directory exists. If no, create a new directory
[ ! -d $downloadsDir ] && mkdir -p $downloadsDir
cd $downloadsDir

#Download, decompress, compile
curl -o redis-stable.tar.gz http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make && make test

#If TCL or higher is required
# yum install -y tcl

#After making, make is recommendedtestAnd I maketestAre killed by FALLING BUILDINGS or TCL 8.5in order to run the Redis test
#Wget HTTP: / / http://prdownloads.sourceforge.net/tcl/tcl8.6.0-src.tar.gz
#Once downloaded, go to the installation directory, go to Unix, and execute"./configure"", then make, then make install

#Copy execution file
cp src/redis-server /usr/local/bin/
cp src/redis-cli /usr/local/bin/

#Create a directory
mkdir /etc/redis
mkdir /var/redis
mkdir /var/redis/log
mkdir /var/redis/run
mkdir /var/redis/port

#Copy the configuration file template from the root directory of the Redis extract
cp redis.conf /etc/redis/redis.conf

#Modifying a Configuration File
echo "################################ WLF DEFINE ##############################" >> /etc/redis/redis.conf
echo "daemonize yes" >> /etc/redis/redis.conf
echo "pidfile /var/redis/run/redis.pid" >> /etc/redis/redis.conf
echo "logfile /var/redis/log/redis.log" >> /etc/redis/redis.conf
echo "dir /var/redis/port" >> /etc/redis/redis.conf
Copy the code

startup.sh

#! /bin/bash

mkdir /usr/local/redis/bin
cp ./dump.rdb /usr/local/redis/bin/dump.rdb
redis-server /etc/redis/redis.conf
Copy the code

changebase.sh

#! /bin/bash
rm ./dump.rdb
cp /usr/local/redis/src/dump.rdb ./dump.rdb
Copy the code

startmock.js

var express = require("express");
var redis = require("redis");
var bodyParser = require("body-parser");

const app = express();
var redisMaster = "127.0.0.1";
var redisClient = redis.createClient({ host: redisMaster, db: 1 });

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

/ / the Router Settings
var vedioTalkRouter = require("./rest/xxx");
app.use("/xx/xxx/xxx/xxxx/xxx", vedioTalkRouter);

app.get("/xx/xxx/xxxx/xxx".function(req, res) {
  redisClient.get("xxx".function(err, items) {
    res.send(items);
  });
});

app.listen(3000);
Copy the code

Start the process

For the first startup, you need to install redis./setup.sh and then nodestartmock. js after /startup.sh

Database baseline adjustment

Note that it needs to be used on the terminal when there are major changes

redis-cli  // Link to local redis by default
127.0. 01.:6379> bgsave   // Save the current database snapshot
cd ./mock_server // CD to the mock_server directory
./changebase.sh / / execution changebase. Sh
Copy the code

RDB in /usr/local/redis/src is copied to the project directory to replace the original RDB and the next time redis-server reads the snapshot into the database

Note: A snapshot of a database of a higher version cannot be read from a database of a lower version. Do not increase the redis version. If the redis version does increase, you need to install Redis-dump for synchronization

Data correction

In the process of developing tests, you can use RDM to view and modify the contents of the database

How was the experience?

Wan said it is better to use, the body sense of this thing, great, mainly has the following advantages

  1. In the development cycle is longer, larger new projects, the front end can accelerate the development progress, simulate the background, basically out of the background
  2. Cultivate the thinking of business logic, if the front end is only UI, and what is the difference between salt fish, in the development of simple background, although it is simple, but will consider the logic and process of business, deepen understanding, reduce bugs
  3. Redis API is simple to use, the database is also quite simple without foreign key cascade, development is like flying