One: Nginx configuration

Registration and login functions are essential to all the systems we encounter. Today we will briefly talk about this process. Mine uses Node as the background, receiving requests from the front end and returning data to the front end, which is in this introductory series. Since front-end browser requests have cross-domain issues, HERE I configure the code with Nginx.

Harpies sometimes
Harpies sometimes

Two: database table establishment

First, you need to register. In this case, you need to have a user table to hold the registration data. We simply need the name, password, mobile phone number, these three fields. For registration and login in the need for authentication, encryption, decryption and so on, not here. Mysql > create TABLE T_user;

Harpies sometimes

Create a unified return object

class Response {
    constructor(isSuc, msg, code, result) {
        this.isSuc = isSuc;
        this.msg = msg;
        this.code = code;
        this.result = result;
    }
}
module.exports = Response;
Copy the code

Four: Register the interface

var logger = require('.. /logConfig');
var connection = require('.. /sqlConfig');
var Response = require('./response');

functionRegister (req, res) {var param = req.body; var username = param.username; var password = param.password; var phone = param.phone; var response = new Response(false.' ', 1);ifConnection.query (username && password && phone) {//1, check whether the same username exists in the database."select * from t_user where username = ?", [username], function (error, results, fields) {
            if (error) throw error;
            ifResults.length >= 1) {//2, if the user has the same user name, the registration fails, the user name repeat response = new response (false.'Registration failed, duplicate user name', 1); // Prints the response message logger.info(response); res.send(response); }else {
                connection.query("insert into t_user(username,password,phone) VALUES(? ,? ,?) ", [username, password, phone], function (error, results, fields) {
                    if(error) throw error; //3. If the user name does not have the same user name and there is a record, the registration is successfulif (results.affectedRows == 1) {
                        response = new Response(false.'Registration successful', 1);
                        logger.info(response);
                        res.send(response);
                    } else {
                        response = new Response(false.'Registration failed', 1); logger.info(response); res.send(response); }}); }})}else {
        response = new Response(false.'Registration failed, user name, password, user number cannot be empty', 1); logger.info(response); res.send(response); } } module.exports = register;Copy the code

Five: Login interface

var logger = require('.. /logConfig');
var connection = require('.. /sqlConfig');
var Response = require('./response');
functionLogin (req, res) {// Print the request message var param = req.body; var username = param.username; var password = param.password; var response = new Response(false.' ', 1);ifConnection.query (username && password) {//1;"select * from t_user where username = ?", [username], function (error, results, fields) {
            if (error) throw error;
            ifResults.length >= 1) {//2. If there is a user name, check whether the password is the sameif(password == results[0].password) {//3, the password is the same as the login success response = new response (true.'Successful landing', 1);
                    logger.info(response);
                    res.send(response);
                } else {
                    response = new Response(true.'Login failed, password error', 1); logger.info(response); res.send(response); }}else {
                response = new Response(false.'Login failed, there is no such user name', 1); logger.info(response); res.send(response); }}); }else {
        response = new Response(false.'Login failed, user name or password cannot be empty', 1); // Prints the response message logger.info(response); res.send(response); } } module.exports = login;Copy the code

Six: create a unified service entrance (subsequent interfaces are written here, will not be listed)

var express = require('express');
var app = express();
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false})) // Set cross-domain access to app.all(The '*'.function (req, res, next) {
    res.header("Access-Control-Allow-Origin"."*");
    res.header("Access-Control-Allow-Headers"."X-Requested-With");
    res.header("Access-Control-Allow-Methods"."PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By".'3.2.1');
    res.header("Content-Type"."application/json; charset=utf-8");
    next();
});
var register = require('./api/register'); Var login = require('./api/login'); / / login app. Post ('/register', (req, res) => register(req, res));
app.post('/login', (req, res) => login(req, res)); // Uncaught errors can be captured globally by uncaughtException. You can also print out the call stack to prevent node from exiting process.on().'uncaughtException'.function(err) {// Prints an error console.log(err); // Print out the error call stack for debugging console.log(err.stack); }); // connection.end(); app.listen(3000,function() {//// listen on port 3000 console.log()'Server running main.js at 3000 port');
});
Copy the code

Seven: the login interface has been written, it is time to verify

At the beginning of this article, the Nginx configuration where the registration interface is displayed, next to look at the login.

Harpies sometimes
Harpies sometimes

Ps: Some of the code is screenshot, may not be copied, I will upload the lastgithub, you can download the code hahaha…)