Home  >  Article  >  Web Front-end  >  What are the differences between nodejs res.end and res.send?

What are the differences between nodejs res.end and res.send?

php中世界最好的语言
php中世界最好的语言Original
2018-05-30 09:33:141747browse

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.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 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!

Statement:
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