Serving non-standard HTTP method with ExpressJS

前端 未结 3 1015
我在风中等你
我在风中等你 2021-01-05 05:57

I would like to write an HTTP server that answer to request using a non-standard HTTP method (verb). For instance, the client would make a request like FOO / HTTP/.1.1

3条回答
  •  無奈伤痛
    2021-01-05 06:30

    Short answer: No, it's not possible. Not without implementing your own HTTP module.

    To test, start a barebones HTTP server ...

    $ node
    > require('http').createServer(function(req, res) {
    ...   console.log(req.method);
    ...   res.end();
    ... }).listen(8080);
    

    Then (as you've already done) telnet to it and issue a GET and FOO request ...

    $ telnet localhost 8080
    Trying ::1...
    telnet: connect to address ::1: Connection refused
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    GET / HTTP/1.1
    
    HTTP/1.1 200 OK
    Connection: keep-alive
    Transfer-Encoding: chunked
    
    0
    
    FOO / HTTP/1.1
    Connection closed by foreign host.
    
    $ 
    

    In node console you'll see

    GET
    

    ... but no FOO. So, node's native HTTP module, which Express uses, does not make these requests available.

提交回复
热议问题