This time I will bring you what are the differences between nodejs res.end and res.send, and what are the precautions for using nodejs res.end and res.send. The following is a practical case, let’s take a look. take a look.
To put it simply, if there is no data from the server to be returned to the client, you can use res.endBut 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);
Detailed explanation of the use of common built-in functions in JS
How to use js to encapsulate ajax function functions and usage
The above is the detailed content of What are the differences between nodejs res.end and res.send?. For more information, please follow other related articles on the PHP Chinese website!