1. Introduce the corresponding module
const express = require('express')
// Create server express module, express framework coexists with native API , So you can use any native method attributes when using this framework
const mysql = require(‘mysql’) // mysql database module
const log4js = require(‘log4js’) // print log module
const path = require(‘path’) // path module
const expressStatic = require('express-static') // introduce static resource module< /span>
const fs = require('fs') // read file module
2. Start the service
let server = express() // Create server let serPort = 8080 // listening port Start: node app.js (the current js file name) Browser access: localhost:8080
Because the mysql database is used here, after installing the mysql module under node, we also need to download the mysql database to the local, and finally use the visualization tool to manipulate the database. Here I recommend Navicat for MySQL
3. Build the database and table and define the data format and data type
4. Use node The connection to the database is as follows
let db = mysql.createPool({host: '127.0.0.1', user:'root',port: 3306,
password: '', database:'xbb_front_tools_test'}) // Create a local connection pool
Here I set the node service to allow cross-domain requests, so that the front-end cannot configure proxy:
res.header(“Access-Control-Allow-Origin”, “*”); // Cross-domain processing
next();
})
5. The interface request I use here is as follows:
Because I use post request by default here, I use req.on('data') and req.on('end') to request
The body is processed, it can be easier if you use get, and the above function is not needed
server.use('/error/v1/ErrorSend', (req, res)=>{ // Insert database
let data ='' // There must be an empty string here (this is because Post can transmit a relatively large amount of data,
When transferring larger data blocks, actually part of the transfer is a complete request body after such splicing)
req.on(‘data’, (query)=>{
data+=query
})
req.on(‘end’, ()=> {
let nextData = JSON.parse(data)
Add here to execute and database interaction code
}
})
6. The unfamiliar point is the addition, deletion, modification, and checking of mysql data, write mysql statement It’s still a bit tricky. You have to understand this when you write it yourself. Here is the code for operating mysql:
//< /span> Insert database
server.use('/error/v1/ErrorSend', (req, res)=>{ // Insert database
let data = ‘‘ // There is an empty string here
req.on(‘data’, (query)=>{
data+=query
})
req.on(‘end’, ()=> {
console.log(data, ‘data’)
let nextData = JSON.parse(data)
try {
if (nextData.type !== undefined && nextData.errorMessage !== undefined && nextData.url !== undefined && nextData.userAgent !== undefined && nextData.platform !== undefined) {
let addTime = new Date().getTime()
db.query(`INSERT INTO receive_error (type,error_name, error_message, corpid, userid, url, user_agent, platform, add_time) VALUES(${nextData.type}, '${nextData.errorName ? nextData.errorName:''}',
'${nextData.errorMessage}','${nextData.corpid? nextData.corpid:''}','${nextData.userid? nextData.userid:''}', ' ${nextData.url}','${nextData.userAgent}','${nextData.platform}', ${addTime})`,
(err, data) =>{
if (err) {
console.log(err)
res.send({msg: ‘The server opens a small difference’, code: 0})
}else {
res.send({msg: ‘Operation successful’, code: 1})
}
res.end()
})
}else {
console.log(123456)
res.send({msg: ‘Request parameters are missing’, code: 0})
res.end()
}
}
catch (err) {
res.send({msg: ‘The server opens a small difference’, code: 0})
res.end()
}
})
})
7. Filter the above saved data and display it in pages
< /p>
The mysql code is as follows:
server.use('/error/v1/receiveErrorLog', (req, res) =>{ // Get list
let data = ‘‘ // must be an empty string
req.on(‘data’, (param) => {
data+=param
})
req.on(‘end’, ()=>{
let nextData = JSON.parse(data)
let queryParam = `SELECT * FROM receive_error WHERE 1=1`
if (nextData.type !== undefined) {
queryParam+=` AND type = ${nextData.type}`
}
if (nextData.errorName !== undefined) {//< /span> When errorName is not empty
queryParam+=` AND error_name LIKE ‘%${nextData.errorName}%‘`
}
if (nextData.errorMessage !== undefined) {
queryParam+=` AND error_message LIKE ‘%${nextData.errorMessage}%‘`
}
if (nextData.corpid !== undefined) {
queryParam+=` AND corpid = ‘${nextData.corpid}‘`
}
if (nextData.userid !== undefined) {
queryParam+=` AND userid = ‘${nextData.userid}‘`
}
if (nextData.url !== undefined) {
queryParam+=` AND url LIKE ‘%${nextData.url}%‘`
}
if (nextData.platform !== undefined) {
queryParam+=` AND platform = ‘${nextData.platform}‘`
}
if (nextData.startTime !== undefined && nextData.endTime !== undefined) {
queryParam+=` AND add_time Between ${nextData.startTime} AND ${nextData.endTime}`
}
if (Object.keys(nextData).length> 2) {
let totalSql = queryParam
sqlPromise(totalSql,res).then((length) => {
queryParam+=` LIMIT ${((--nextData.currentPage) * nextData.pageSize)}, ${nextData.pageSize}`
filterQuery(queryParam, length, res)
}, (err) =>{
console.log(err)
})
}
if (Object.keys(nextData).length === 2) {// Only the pageSize and currentPage are passed for the initialization request
queryParam = `SELECT * FROM receive_error LIMIT ${((--nextData.currentPage) * nextData.pageSize)}, ${nextData.pageSize}`
let totalSql = `SELECT * FROM receive_error`
sqlPromise(totalSql,res).then((length) => {
filterQuery(queryParam, length, res)
}, (err) => {console.log(err)})
}
})
})
Personal opinions, welcome to give pointers! ! !
const express = require(‘express‘)
// Create server express module, express framework coexists with native API , So you can use any native method attributes when using this framework
const mysql = require(‘mysql’) // mysql database module
const log4js = require(‘log4js’) // print log module
const path = require(‘path’) // path module
const expressStatic = require('express-static') // introduce static resource module< /span>
const fs = require('fs') // read file module
let server = express() // Create server let serPort = 8080 // listening port Start: node app.js (the current js file name) Browser access: localhost:8080
let db = mysql.createPool({host: '127.0.0.1', user:'root',port: 3306,
password: '', database:'xbb_front_tools_test'}) // Create a local connection pool
server.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // Cross-domain processing
next();
})
Because I use post request by default here, I use req.on('data') and req.on('end ') on request
The body is processed, it can be easier if you use get, and the above function is not needed
server.use('/error/v1/ErrorSend', (req, res)=>{ // Insert database
let data ='' // There must be an empty string here (this is because Post can transmit a relatively large amount of data,
When transferring larger data blocks, actually part of the transfer is a complete request body after such splicing)
req.on(‘data’, (query)=>{
data+=query
})
req.on(‘end’, ()=> {
let nextData = JSON.parse(data)
Add here to execute and database interaction code
}
})
// Insert database
server.use('/error/v1/ErrorSend', (req, res)=>{ // Insert database
let data = ‘‘ // There is an empty string here
req.on(‘data’, (query)=>{
data+=query
})
req.on(‘end’, ()=> {
console.log(data, ‘data’)
let nextData = JSON.parse(data)
try {
if (nextData.type !== undefined && nextData.errorMessage !== undefined && nextData.url !== undefined && nextData.userAgent !== undefined && nextData.platform !== undefined) {
let addTime = new Date().getTime()
db.query(`INSERT INTO receive_error (type,error_name, error_message, corpid, userid, url, user_agent, platform, add_time) VALUES(${nextData.type}, '${nextData.errorName ? nextData.errorName:''}',
'${nextData.errorMessage}','${nextData.corpid? nextData.corpid:''}','${nextData.userid? nextData.userid:''}', ' ${nextData.url}','${nextData.userAgent}','${nextData.platform}', ${addTime})`,
(err, data) =>{
if (err) {
console.log(err)
res.send({msg: ‘The server opens a small difference’, code: 0})
}else {
res.send({msg: ‘Operation successful’, code: 1})
}
res.end()
})
}else {
console.log(123456)
res.send({msg: ‘Request parameters are missing’, code: 0})
res.end()
}
}
catch (err) {
res.send({msg: ‘The server opens a small difference’, code: 0})
res.end()
}
})
})
server.use('/error/v1/receiveErrorLog', (req, res) =>{ // Get list
let data = ‘‘ // must be an empty string
req.on(‘data’, (param) => {
data+=param
})
req.on(‘end’, ()=>{
let nextData = JSON.parse(data)
let queryParam = `SELECT * FROM receive_error WHERE 1=1`
if (nextData.type !== undefined) {
queryParam+=` AND type = ${nextData.type}`
}
if (nextData.errorName !== undefined) {//< /span> When errorName is not empty
queryParam+=` AND error_name LIKE ‘%${nextData.errorName}%‘`
}
if (nextData.errorMessage !== undefined) {
queryParam+=` AND error_message LIKE ‘%${nextData.errorMessage}%‘`
}
if (nextData.corpid !== undefined) {
queryParam+=` AND corpid = ‘${nextData.corpid}‘`
}
if (nextData.userid !== undefined) {
queryParam+=` AND userid = ‘${nextData.userid}‘`
}
if (nextData.url !== undefined) {
queryParam+=` AND url LIKE ‘%${nextData.url}%‘`
}
if (nextData.platform !== undefined) {
queryParam+=` AND platform = ‘${nextData.platform}‘`
}
if (nextData.startTime !== undefined && nextData.endTime !== undefined) {
queryParam+=` AND add_time Between ${nextData.startTime} AND ${nextData.endTime}`
}
if (Object.keys(nextData).length> 2) {
let totalSql = queryParam
sqlPromise(totalSql,res).then((length) => {
queryParam+=` LIMIT ${((--nextData.currentPage) * nextData.pageSize)}, ${nextData.pageSize}`
filterQuery(queryParam, length, res)
}, (err) =>{
console.log(err)
})
}
if (Object.keys(nextData).length === 2) {// Only the pageSize and currentPage are passed for the initialization request
queryParam = `SELECT * FROM receive_error LIMIT ${((--nextData.currentPage) * nextData.pageSize)}, ${nextData.pageSize}`
let totalSql = `SELECT * FROM receive_error`
sqlPromise(totalSql,res).then((length) => {
filterQuery(queryParam, length, res)
}, (err) => {console.log(err)})
}
})
})