This time I will bring you an analysis of the differences between nodejs res.end and res.send. What are the precautions when using nodejs res.end and res.send. The following is a practical case. Let’s take a look. one time.
To put it simply, if there is no data from the server to be returned to the client, you can use res.end
But if there is data from the server to be returned to the client, you must use res.send. You cannot use res.end (an error will be reported)
Example:
var express = require('express'); var app = express(); var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : 'root', port : 3306, database : 'test' }) sql = 'select * from websites'; var arr = []; connection.query(sql,function (err, results) { if (err){ console.log(err) }else{ console.log(results); for(var i = 0;i < results.length;i++){ arr[i] = results[i].name; } app.get('/',function (req, res) { res.send(arr); //这里必须用res.send,因为有数据返回到客户端 }) } }) app.listen(3001);
I believe you have mastered the method after reading the case in this article, and there will be more exciting things. Please pay attention to other related articles on php Chinese website!
Recommended reading:
Detailed explanation of the steps to introduce js numeric keypad in vue
jQuery class name selector (.class) Detailed explanation of how to use
The above is the detailed content of Difference analysis between nodejs res.end and res.send. For more information, please follow other related articles on the PHP Chinese website!