Function self calling
In general, the purpose of taking a function as a parameter is to obtain the asynchronous operation results inside the function
-
The following code is an example of synchronization
function add (x + y) { // Function method return x + y } add(10, 20) // Calling function
-
The following code is an asynchronous function
console.log('setTimeout external before') setTimeout(function () { console.log('setTimeout inside before') console.log('setTimeout inside after') }, 0) console.log('setTimeout external after')
Since setTimeout is an asynchronous function, the browser will not wait (even if the execution time is 0) for the function to execute at runtime, so the final output is
setTimeout external before setTimeout external after setTimeout inside before setTimeout inside after
This is because javascript is single threaded
-
Get the operation result inside the asynchronous function
function add (x, y) { setTimeout(function () { var ret = x + y }, 0) }
How to get the value of ret in the function internal setTimeout asynchronous function? Let's start with two error cases
function add (x, y) { setTimeout(function () { var ret = x + y return ret }, 0) } add(10, 20)
The operation result is Uncaught ReferenceError: ret is not defined
The reason is that the value returned by ret in setTimeout is not received by variables in the add function
function add (x, y) { var ret setTimeout(function () { ret = x + y }, 0) return ret } add(10, 20)
The operation result is undefined
The reason is that the execution order of the setTimeout asynchronous function is var return return return and setTimeout is last, so the result is undefined
Get the result of asynchronous method inside function through callback function
function add (x, y, callback) { setTimeout(function () { var ret = x + y callback(ret) }, 0) } add(10, 20, (val) => { console.log(val) })
The operation result is undefined 30
In the code, the calling method add() is composed of three parameters, the first two parameters are passed to the asynchronous method setTimeout, ret = x + y is used to calculate, the third parameter is a function, this function parameter is the parameter, receives the ret final operation result as the real parameter.
In this way, the asynchronous method running results inside the function can be thrown for external use
CRUD static routing setup
-
First, you need several static pages:
index.html is used to display the homepage (information list) display page
new.html for new information page
edit.html is used to modify the page with existing information
Static pages need to be placed in views for express to use
-
Create a data JSON file named db.json:
{ "students": [{ "id": 4, "name":"Zhang San San", "gender":"0", "age":"22" }] }
-
New project:
npm init -y // Quick build project
-
Create the entry file app.js, introduce the required components and configuration
// npm install express --save var express = require('express') // Introducing express // npm i art-template app.engine('art', require('express-art-template')) // Configure express art template // npm install --save body-parser var bodyParser = require('body-parser') // Introduce body parser app.use(bodyParser.urlencoded({ // Configure body parser extended: false })) app.use(bodyParser.json())
-
Create route (in order to better reflect modular development, the route module is now proposed to router.js separately)
There are two ways to build in 14nodejs (7 days)
-
Pass app as a parameter to router.js (not recommended)
// route.js module.expotrs = (app) => { app.get('/', (req, res) => {}) // Render home page app.get('/new', (req, res) => {}) // Render add information page app.post('/new', (req, res) => {}) // Submit added information app.get('/edit', (req, res) => {}) // Rendering information editing page app.post('/edit', (req, res) => {}) // Submit edited information app.get('/delete', (req, res) => {}) // Delete information } // app.js var express = require('express') // Introducing express var router = require('./route') // Introduce router.js var app = express() router(app) // Pass app as a parameter to router.js
-
Use Express to wrap routing (recommended)
// route.js var express = require('express') // Introducing express var router = express.Router() // Use the router method in express to create a routing container // Hang the route on the router router.get('/', (req, res) => {}) // Render home page router.get('/new', (req, res) => {}) // Render add information page router.post('/new', (req, res) => {}) // Submit added information router.get('/edit', (req, res) => {}) // Rendering information editing page router.post('/edit', (req, res) => {}) // Submit edited information router.get('/delete', (req, res) => {}) // Delete information module.exports = router // Export router container // app.js var express = require('express') // Introducing express var router = require('./route') // Introduce router.js var app = express() app.use(router) // Hang the routing container in the app service
-
-
Create Server service
app.use('/public/', express.static('./public/')) // Open public directory app.use(router) // Hang in router routing file app.listen('2020', () => { // Bind server port number console.log('Server started successfully') })
So far, the jump of static files is over. Next, we will develop the function of adding, deleting, modifying and querying one by one
CRUD addition, deletion, modification and query
Encapsulate all the methods related to addition, deletion, modification and query in the student.js file
All methods are exported through exports
Finally, the student.js file is imported into router.js for routing
Method to export the callback result through function self call
-
increase
// student.js var fs = require('fs') // Introduce core component fs var dbPath = './db.json' // Import original data file exports.add = (student, callback) => { fs.readFile(dbPath, 'utf-8', (err, data) => { if (err) { return callback(err) // If the db.json file fails to read, an err is thrown out } var students = JSON.parse(data).students // Convert the JSON text data object of dbPath student.id = students[students.length - 1].id + 1 students.push(student) // Append form data to students var fileData = JSON.stringify({ // Convert objects back to text students: students }) fs.writeFile(dbPath, fileData, (err) => { if (err) { // If the write to file fails, an err is thrown out return callback(err) } callback(null) // Write success, no action }) }) } // router.js var Student = require('./student') // Import student.js Student.add(req.body, (err) => {}) // The add method takes two parameters // Parameter 1 (Object) receives the data Object to be added // Parameter 2 (Function) the Function parameter receives err
-
Delete
// student.js var fs = require('fs') // Introduce core component fs var dbPath = './db.json' // Import original data file exports.deleteId = function (id, callback) { fs.readFile(dbPath, 'utf8', function (err, data) { if (err) { return callback(err) // If the db.json file fails to read, an err is thrown out } var students = JSON.parse(data).students // Convert the JSON text data object of dbPath var deleteId = students.findIndex(function (item) { // The findIndex method is specifically used to find the subscript of an element based on a condition return item.id === parseInt(id) }) students.splice(deleteId, 1) // Delete the corresponding student object from the array according to the subscript var fileData = JSON.stringify({ // Convert object data to strings students: students }) fs.writeFile(dbPath, fileData, function (err) { if (err) { // If the write to file fails, an err is thrown out return callback(err) } callback(null) // Write success, no action }) }) } // router.js var Student = require('./student') // Import student.js Student.deleteId(req.query.id, function (err) {}) // The deleteId method receives two parameters // Parameter 1 (String) receives the data id used for matching // Parameter 2 (Function) the Function parameter receives err
-
change
// student.js var fs = require('fs') // Introduce core component fs var dbPath = './db.json' // Import original data file exports.edit = (student, callback) => { fs.readFile(dbPath, 'utf-8', (err, data) => { if (err) { return callback(err) // If the db.json file fails to read, an err is thrown out } var students = JSON.parse(data).students // Convert the JSON text data object of dbPath var queryId = students.find((item) => { // Find the matching object in dbPath through req.body.id return item.id === parseInt(student.id) }) for (var key in student) { // Replace the object to be modified queryId[key] = student[key] } var fileData = JSON.stringify({ // Convert objects back to text students: students }) fs.writeFile(dbPath, fileData, (err) => { if (err) { // If the write to file fails, an err is thrown out return callback(err) } callback(null) // Write success, no action }) }) } // router.js var Student = require('./student') // Import student.js Student.edit(req.body.id, (err) => {}) // The edit method takes two parameters // Parameter 1 (String) receives the data id used for matching // Parameter 2 (Function) the Function parameter receives err
-
Check (all)
// student.js var fs = require('fs') // Introduce core component fs var dbPath = './db.json' // Import original data file exports.findAll = (callback) => { fs.readFile(dbPath, 'utf-8', (err, data) => { if (err) { return callback(err) // If the db.json file fails to read, an err is thrown out } callback(null, JSON.parse(data).students) // Throw data if read successfully }) } // router.js var Student = require('./student') // Import student.js Student.findAll((err, students) => {}) // The find method takes a Function parameter, which has two more parameters // Parameter 1 receive err // Parameter 2 receives real data (JSON.parse(data).students)
-
Check (single)
// student.js var fs = require('fs') // Introduce core component fs var dbPath = './db.json' // Import original data file exports.findId = (queryId, callback) => { fs.readFile(dbPath, 'utf-8', (err, data) => { if (err) { return callback(err) // If the db.json file fails to read, an err is thrown out } var students = JSON.parse(data).students // Convert the JSON text data object of dbPath var student = students.find((item) => { // Find the matching object in dbPath through queryId return item.id === parseInt(queryId) }) return callback(null, student) }) } // router.js var Student = require('./student') // Import student.js Student.findId(req.query.id, (err, data) => {}) // The findId method takes two parameters // Parameter 1 (String) receives the data id used for matching // Parameter 2 (Function) this Function has two parameters, one is err, the other is real data
Apply the method to router.js, and the final router.js page code is
var fs = require('fs') // Introduce core component fs var Student = require('./student') // Import student.js var express = require('express') // Import express var router = express.Router() // express package routing router.get('/', (req, res) => { // Render home page Student.findAll((err, students) => { if (err) { // If findAll throws an error, the Server error will be reported, and the status code is 500 return res.status(500).send('Server error.') } res.render('index.html', { // If the data is read successfully, render the first page students: students }) }) }) router.get('/new', (req, res) => { // Render add information page if (err) { // If findAll throws an error, the Server error will be reported, and the status code is 500 return res.status(500).send('Server error.') } res.render('new.html') }) router.post('/new', (req, res) => { // Submit added information Student.add(req.body, err => { // Add req.body object to. / db.json if (err) { // If findAll throws an error, the Server error will be reported, and the status code is 500 return res.status(500).send('Server error.') } res.redirect('/') // If it is added successfully, the page will be redirected back to the first page }) }) router.get('/edit', (req, res) => { // Rendering information editing page Student.findId(req.query.id, (err, student) => { // Match data in. / db.json through req.query.id if (err) { // If findAll throws an error, the Server error will be reported, and the status code is 500 return res.status(500).send('Server error.') } res.render('edit.html', { // Render the edit page of the current data student: student }) }) }) router.post('/edit', (req, res) => { // Submit edited information Student.edit(req.body, (err) => { // Match the data in. / db.json through req.body and replace it if (err) { // If findAll throws an error, the Server error will be reported, and the status code is 500 return res.status(500).send('Server error.') } res.redirect('/') // If it is added successfully, the page will be redirected back to the first page }) }) router.get('/delete', (req, res) => { // Delete information Student.deleteId(req.query.id, function (err) { // Match the data in. / db.json through req.query.id and delete it if (err) { // If findAll throws an error, the Server error will be reported, and the status code is 500 return res.status(500).send('Server error.') } res.redirect('/') // If it is added successfully, the page will be redirected back to the first page }) }) module.exports = router // Export router container
Article synced to my personal blog:< Node learning record function self calling and crud addition, deletion, modification and query>
Reference: