Home > Web Front-end > JS Tutorial > body text

Difference analysis between nodejs res.end and res.send

php中世界最好的语言
Release: 2018-05-21 15:19:15
Original
3561 people have browsed it

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template