Home  >  Article  >  Web Front-end  >  How to build a mini program background service in Node.js

How to build a mini program background service in Node.js

亚连
亚连Original
2018-06-13 18:13:443356browse

Recently I am developing a WeChat application account applet. The background data interface of the applet requires https secure request, so my nodejs server needs to be able to provide https support. Now I will talk about the entire https server building process

Preparation conditions

One server

SSL certificate, the mini program requires https protocol

Server background

Node.js
express
mongodb
pm2

The following takes Tencent Cloud Host (centos) as an example

Install Node.js

yum install nodejs

You can also install nvm and use nvm to manage the nodejs version

Install git

yum install git

Connect to the remote code base

Take github as an example:

Configure user Information

git config --global user.name youname
git config --global user.email youemail

Generate ssh public key

ssh -keygen -t rsa -C youemail

The default generated directory is/ root/.ssh
Find the id_rsa.pub file in the directory, copy the contents, and add ssh to your github

Install mongodb and client shell

yum install mongodb-server mongodb -y

Create the database file storage directory

mkdir -p /data/mongodb
mkdir -p /data/logs/mongodb

Start the mongodb database service

mongod --fork --dbpath /data/mongodb --logpath /data/logs/youlog.log

Note:
If you add after the command when starting the mongodb service --auth will enable authentication.
It is recommended to enable authentication. If you do not hold a meeting, you will easily be hacked.

--port 12345 can change the port number of the database, the default is 27017.

--fork starts the database service as a daemon process.

--dbpath /data/mongodb specifies the directory where the database files are stored.

--logpath /data/logs/youlog.log Specifies the log file directory.

The configuration file of mongodb is in /etc/mongod.conf by default

Start the client shell

mongo //Start the mongodb client shell default connection test database

You can switch database connections and perform related operations in the shell.

After the cloud server starts the database service as a daemon, close the terminal and the service will not be terminated. So there is no need to start the database service next time, just connect directly.

Close the database service

mongod --shutdown (--dbpath /data/mongodb)

If added at startup If dbpath is not the default /data/db, dbpath should also be added when closing the database service.

Create https service

npm init //Project initialization

Install express

npm install express --save

Implementing a simple https server

const https = require('https');
const fs = require('fs');
const express = require('express');
const app = express();

let key = fs.readFileSync('youssl.key');
let cert = fs.readFileSync('youssl.crt');

let options = {
  key : key,
  cert : cert
};

const httpsServer = https.createServer(options,app);
httpsServer.listen(443, () => {
  console.log('listening 443 port');
});

app.get('/',(req, res, next) => {
  console.log('someone request');
});

.key and .cert files are your ssl authentication files, using Tencent Cloud Host For example, you can have a free 1-year ssl certificate.

Use mongoose to operate the database

npm install mongoose --save
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/dbname'); //连接数据库

const connection = mongoose.connection;
connection.once('open', (err) => {
  if(err){
    console.log('Database connection failure');
  }else{
    console.log('Database opened');
  }
});

const Schema = mongoose.Schema;

const YourSchema = new Schema({
  name : String,
  age : Number,
});

const yourModel = mongoose.model('yourtable', YourSchema); //在数据库中对应的表为yourtables

let yourDoc = new yourModel({
  name : 'yourname',
  age : 18,
});

Note that if you add --auth when starting the mongodb database service, you must add an authenticated account when using mongoose to connect to the database

mongoose.connect('mongodb://youraccount:pwd@127.0.0.1/dbname');

The corresponding relationship between mongodb and relational database

Schema is equivalent to the structure of the table. It can predefine the field types of the document and cannot perform database operations. Modle can perform a series of database operations, equivalent to tables. An instance of Model is equivalent to a row of the table.

Use pm2

Install pm2

npm install -g pm2

Start the application

pm2 start app.js

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to compile, package and view the index file in vue

How to use Jade template in vue

Passing templates to components in Angular

The above is the detailed content of How to build a mini program background service in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn