Home  >  Article  >  Web Front-end  >  Detailed explanation of the steps to use render and send in node.js

Detailed explanation of the steps to use render and send in node.js

php中世界最好的语言
php中世界最好的语言Original
2018-05-21 15:25:392493browse

This time I will bring you a detailed explanation of the steps for using render and send in node.js. What are the precautions for using render and send in node.js? The following is a practical case. , let’s 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 help us set the Content-Type header and 200status code 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!');

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

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

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);

send:

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

I believe I read this case You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of jQuery element selector use cases

Summary of js common dom node operation usage methods

The above is the detailed content of Detailed explanation of the steps to use render and send in node.js. 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