【Express.js】response

response

After talking about the request in the previous section, let's talk about the response in this section!
This section, as the primary content, will list the more commonly used response methods and their simple usage forms

Preparation

Copy the first section of the Hello World project

status

res.statue(status code) , this is a very practical method to set the Http status code of the response body, although the REST-Apid style is uniform 200, but in express.js, you must set the status code in some situations for a value

res.status(404);

send

res.send(content) , put any type of content in the response body and return it to the request source

app.get('/', function (req, res) {
    
    
    res.send('Hello World!');
});

It can also be followed by status:

res.status(200).send(<p>hello world</p>);

end

res.end() , used to quickly end the response in the scenario that does not need to return data, but end() can also transmit data, but it consumes a lot of performance, it is not recommended to use res.end to transmit data and information

//END
app.get('/end',(req,res)=>{
    
    
    res.status(404).end();
})

json

res.json(content) returns the request body in json format to the request source, and may be restricted by cross-domain protection, so we often need a string of Allow set on top of us

//json - 同源限制
app.get('/json',(req,res)=>{
    
    
    let resp = {
    
    
        code: 200,
        msg: "json",
        toPrint: function(){
    
    
            console.log(`resp[code: ${
      
      this.code}, msg: ${
      
      this.msg}]`);
        }
    }
    res.jsonp(resp);
})

jsonp

res.jsonp(content) , jsonp is an unorthodox data format developed by developers. It is basically the same as json, but it is unlikely to be blocked by cross-domain protection

//jsonp - 更容易的解决跨域
app.get('/jsonp',(req,res)=>{
    
    
    let resp = {
    
    
        code: 200,
        msg: "jsonp",
        toPrint: function(){
    
    
            console.log(`resp[code: ${
      
      this.code}, msg: ${
      
      this.msg}]`);
        }
    }
    res.jsonp(resp);
})

sendFile

res.sendFile(path,options) , transfer files statically, you cannot directly use relative paths

//sendfile,传输文件
app.get('/sendfile',(req,res)=>{
    
    
    //只能是静态路径,需要相对路径的话有很多方法,以下是其中一个
    res.sendFile("hello.txt", {
    
    root: __dirname});
})

download

res.download(path) , download and transfer files, this can directly use the relative path

//download,下载文件
app.get('/download',(req,res)=>{
    
    
    //可以直接相对路径
    res.download("./hello.txt");
})

sendStatus

res.sendStatus(status code) , set the response code, and return the text preset for the status code, such as res.sendStatus(404), the response status code is s404, and the returned information is 'not Found'

//sendstatus
app.get('/sendstatus',(req,res)=>{
    
    
    //预先写好的httpStatus组合,改一下试试,如果你输入的status码不存在,msg将变成这个码的数字
    res.sendStatus(404);
})

location

res.location(route) For forwarding between REST interfaces, the status must be set to a number between 300 and 309 to successfully forward

//location
app.get('/location/1',(req,res)=>{
    
    
    //转发到最早的helloworld路由去
    res.location('/').status(302).send();
})

//location/2
app.get('/location/2',(req,res)=>{
    
    
    //转达到下面定义的这个接口
    res.location('/location/index').status(302).send();
})

app.get('/location/index',(req,res)=>{
    
    
    res.send("welcome to learn express.js by demos");
})

redirect

res.redirect() , routing redirection, this is quite special, we will talk about it in the routing control section

Next section-Sql-knex addition, deletion, modification and query

Guess you like

Origin blog.csdn.net/m0_51810668/article/details/131277978