Drop request in node.js express

前端 未结 3 1076
半阙折子戏
半阙折子戏 2021-02-14 23:02

Is it possible using Node.js and express to drop a request for certain route? I.E. not return a http status or any headers? I\'d like to just close the connection.



        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-14 23:46

    Yes you can. All you need to do is call the res.end method optionally passing in the status code.

    Use one of the following methods:

    res.end();
    res.status(404).end();
    

    If you wanted to also set the headers, then you'd use the res.set method. See below

    res.set('Content-Type', 'text/plain');
    
    res.set({
      'Content-Type': 'text/plain',
      'Content-Length': '123',
      'ETag': '12345'
    })
    

    For details have a look here http://expressjs.com/api.html

提交回复
热议问题