hapi framework building record: initialization project

  • hapi framework, using Official website In short, Hapi is a rich framework for building applications and services, enabling developers to focus on writing reusable application logic rather than spending time building infrastructure.
  • In my own words, it is a node service infrastructure like express and koa.
  • This blog has read Nuggets Practice Guide for developing Node.js applet backend based on hapi After the practice, the practice process and the stepped pit are recorded. Interested readers can support reading the original edition of nuggets.

The first helloworld

1. Initialize the configuration file in the new project directory

npm init

2. install hapi

npm i hapi

3. Global installation hot deployment tool Supervisor (similar to nodemon)

npm install supervisor -g

4. Create a new app.js from Official website case Sample code of Chinese copy

'use strict';

const Hapi = require('hapi');

const init = async () => {

    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });

    server.route({
        method: 'GET',
        path:'/',
        handler: (request, h) => {

            return 'Hello World!';
        }
    });

    await server.start();
    console.log('Server running on %ss', server.info.uri);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();

5. Terminal operation project

6. Browser test effect

Directory structure division

(PS: business logic code can be subdivided into controllers according to needs)

├── config                       # Project configuration directory
├── logs                         # Output log
├── migrations                   # Create database file
├── models                       # Database model
├── node_modules                 # The dependent directory of node.js
├── plugins                      # Plugin directory
├── routes                       # Routing directory
├── seeders                      # Initialize table data file
├── test                         # Test class
├── utils                        # Tools related directory
├── .env                         # configuration file
├── app.js                       # Project entry file
├── package.json                 # JS project engineering dependency Library
├── readme.md                    # Instruction manual on how to use project engineering

Configure global environment information

1. env configuration example

The significance of configuring env is that the domain name database and other information in different environments are not necessarily consistent, and it is more flexible to load and read through configuration means. At the same time, the global environment is obtained directly.

# Domain name configuration information
HOST = 127.0.0.1
PORT = 3303

# MySQL database connection configuration information
MYSQL_HOST = 127.0.0.1
MYSQL_PORT = 3306
MYSQL_DB_NAME = database_name
MYSQL_USERNAME = database_username
MYSQL_PASSWORD = database_password

# JWT custom secret
JWT_SECRET = your_secret

# Wechat applet configuration
WX_APPID = your-app-id # Wechat app ID
WX_SECRET = your-secret # Wechat applet password
WX_MCHID = your-mchid # Payment merchant No
WX_PAY_API_KEY = your-pay-api-key # api key of wechat payment

2. install env2

Through this dependency, you can directly read the parameter information of. env configuration in js code (for example, read the port number process.env.PORT)

npm i env2

3. Sample acquisition

4. effect

Tags: MySQL npm Database supervisor JSON

Posted on Thu, 21 Nov 2019 10:40:07 -0500 by brainstem