Since many friends do not understand or have some problems with the background node startup in the CMS system I wrote in the last article, HERE I will specifically write the steps and details of the startup. Those who are interested in CMS full-stack system can see the two articles I wrote before:

1. Implement a CMS full stack project from 0 to 1 based on nodeJS (Part 1)

2. Implement a CMS full stack project from 0 to 1 based on nodeJS (middle)

Abstract

This paper mainly introduces the following contents:

  • Install redis and start the Redis server
  • Startup of the Node server and configuration of the development and formal environments
  • Server interface testing and using Postman to test the interface

This is a very short article, and I hope you can do a better job of developing node and the front end.

1. Install redis and start redis server

We can go to the Official website of Redis to download the redis installation program, I mainly introduce the installation of Windows and Linux system, if you are other systems, you can go to the official website to check the relevant information.

  1. Install and start the service in window

Download at github.com/MSOpenTech/…

Redis supports both 32-bit and 64-bit. Depending on your platform, download the package to drive C, unzip it and rename the folder redis. (It’s easy to remember here)

redis-server.exe redis.windows.conf
Copy the code

If the following information is displayed, the startup is successful:

If we want to test whether redis can be executed or not, we must not close the CMD window we started above. Otherwise, we will see the following error:

All right, let’s go ahead and test if Redis is available. First, let’s create a new CMD window and switch to the redis directory to run:

redis-cli.exe -h 127.0. 01. -p 6379
Copy the code

Set key-value pairs:

set user xxx
Copy the code

The following result will appear:

In this way, Redis can start the piece.

  1. Install and start Redis on Linux

IO /download. You are advised to…

$wget http://download.redis.io/releases/redis-2.8.17.tar.gz $tar XZF redis - 2.8.17. Tar. Gz $cdRedis - 2.8.17 $makeCopy the code

After make is complete, the compiled redis service program redis-server and the test client program redis-cli are displayed in the redis-2.8.17 directory. The two programs are located in the SRC directory.

$ cd src
$ ./redis-server
Copy the code

If you want to learn more about the configuration and startup of Redis, please read the redis official document carefully.

2. Start the Node server and configure the development environment and the official environment

I talked briefly about compiling and configuring Node in the last article, but here we show you how to start the project and package the online and environment code.

First let’s look at the package.json execution code:

"scripts": {
   "start": "export NODE_ENV=development && nodemon -w src --exec \"babel-node src\""."build": "babel src --out-dir dist"."run-build": "node dist"."test": "echo \"Error: no test specified\" && exit 1"
 }
Copy the code

Export NODE_ENV=development (); export NODE_ENV=development ();

Set NODE_ENV=development We start it with the following command:

npm start
// or yarn start
Copy the code

Next, let’s look at the build command, which is mainly the command to package the output. Dist is the directory name of the output, or you can name it according to your code style.

We do this as follows:

npm run build
// or yarn build
Copy the code

The run-build command is an extra I added to make it easier to test the production code, which is the code we packaged. Execution is the same as above.

For details on how to use environment variables, we can refer to the code under config in my project:

const isDev = process.env.NODE_ENV === 'development';

// Obtain the local IP address
function getIPAdress() {
   var interfaces = require('os').networkInterfaces(); &emsp; &emsp;for (var devName ininterfaces) {&emsp; &emsp; &emsp; &emsp;variface = interfaces[devName]; &emsp; &emsp; &emsp; &emsp; &emsp; &emsp;for (var i = 0; i < iface.length; i++) {
           var alias = iface[i];
           if (alias.family === 'IPv4'&& alias.address ! = ='127.0.0.1' && !alias.internal) {
               return alias.address;
           }
       }&emsp;&emsp;
   }
}

const IP = getIPAdress();
const staticPath = isDev ? `http://${IP}: 3000 ` : 'Online address';


module.exports = {
   isDev,
   staticPath
}
Copy the code

We can use process.env.node_env to take the environment variable we defined in the package and do different things. The main function of the above code is to pass environment variables to other business code, and use different IP addresses for static paths according to different environments, so as to facilitate front-end debugging.

3. Server interface test and use Postman to test the interface

After we start the server, we can use IP to test the port. Since config and admin data will be initialized during initialization, we can use Postman to test the reliability of the interface. In order to facilitate understanding, I will split the initialization code.

// server/src/db/schema/config.js
// ...
// Initialize the config data
async function initConfig(){
   const isExist = await configSchema.exists()
   if(! isExist) {const result = await configSchema.hmset(null, {
           header: {
               columns: ['home'].height: '50'.backgroundColor: '# 000000'.logo: ' '
           },
           banner: {
               type: '1'.// 0 is the tag cloud, and 1 is the rotation diagram
               label: [],
               bgUrl: ' '.bannerList: []},bannerSider: {
               tit: 'Sidebar Information'.imgUrl: ' '.desc: ' '
           },
           supportPay: {
               tit: ' '.imgUrl: ' '}})if(!Array.isArray(result)) {
           console.log('Configuration information initialization completed')}else {
           throw result
       }
   }
}

initConfig()
Copy the code

We in/SRC/db/schema/server config. Js can see under the initialization config code, then look at the route definition:

conclusion

In summary, our server startup process is as follows:

  1. Start the Redis server
  2. Run the node startup script NPM start or yarn start
  3. Test the port using either Postman or local request, based on the routing API defined by the router

The last

Due to the move today, the time has not been arranged, tomorrow will launch the rest of the server, CMS full stack management background and client part of the implementation. Include:

  • Implement custom KOA middleware and restful apis
  • Koa routing and Service layer implementation
  • Basic use and skills of the template engine PUG
  • Vue management background page implementation and source sharing
  • React client foreground specific implementation and source sharing
  • Pm2 deployment and nGINx server configuration

Project full source address I will tell you before eleven, welcome in the public number “interesting talk front end” to join us to discuss.

More recommended

  • Implementing a CMS full stack project from 0 to 1 based on nodeJS (Part 1)
  • “Javascript advanced programming” core knowledge summary
  • With CSS3 to achieve stunning interviewers background that background animation (advanced source)
  • Remember an old project with cross-page communication issues and front-end implementation of file downloads
  • Write a mock data server using nodeJS in 5 minutes
  • JavaScript binary tree and binary search tree implementation and application
  • With JavaScript and C3 to achieve a turntable small game
  • Teach you to use 200 lines of code to write a love bean spell H5 small game (with source code)
  • Exploration and summary of front-end integrated solutions based on React/VUE ecology
  • How to write your own JS library in less than 200 lines of code
  • A picture shows you how to play vue-Cli3 quickly
  • 3 minutes teach you to use native JS implementation with progress listening file upload preview component
  • Developing travel List with Angular8 and Baidu Maps API
  • Js basic search algorithm implementation and 1.7 million data under the performance test
  • How to make front-end code 60 times faster
  • Vue Advanced Advanced series – Play with Vue and vuex in typescript