Home > Web Front-end > JS Tutorial > About the difference and usage of render and send in node.js (code attached)

About the difference and usage of render and send in node.js (code attached)

亚连
Release: 2018-05-18 13:39:16
Original
2097 people have browsed it

The following is what I have compiled for you about the differences and usage methods of render and send in node.js. Interested students can take a look.

In most cases, res.render() is used to render content, which will be rendered according to the template file in views. If you don’t want to use the views folder and want to set the folder name yourself, then app.set("views","aaaa");

If you want to write a quick test page, of course you can use res.send() . This function will automatically set the Content-Type header and 200 status code for us based on the content. send() can only be used once, the same as end. How is it different from end? Ability to automatically set MIME types.

If you want to use different status codes, you can:

   res.status(404).send('Sorry, we cannot find that!');
Copy after login

If you want to use different Content-Types, you can:

   res.set('Content-Type', 'text/html');
Copy after login

render:

var express = require("express");  
var app = express();  
app.set("view engine", "ejs");  
app.get("/", function (req, res) {  
    res.render("haha", {news:[]});  
});  
app.listen(3000);
Copy after login

send:

app.get("/check", function (req, res) {  
    res.set('Content-Type', 'text/html');  
    res.send({  
        "user" : "ok"  
    });  
});
Copy after login

The above is what I have compiled for you about the differences and usage methods of render and send in node.js. I hope it will be helpful to you in the future.

Related articles:

How to reference parent data inside the JsRender loop (the code is attached and will be introduced to you in detail)

How to establish a model data model in js (the code is provided, simple and easy to understand)

Simple operation of JS download file stream (code attached)

The above is the detailed content of About the difference and usage of render and send in node.js (code attached). 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