Serving a custom HTTP method with ExpressJS

I would like to write an HTTP server that responds to a request using a non-standard HTTP method (verb). For example, a client would execute a type request FOO / HTTP/.1.1. And on the server side, this request will be processed with something like:

var express = require('express');

var app = express.createServer();

app.configure(function(){
  app.use(express.logger({ format: ':method :url' }));
  app.use(express.methodOverride());
});

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

app.listen(3000);

I added my custom method to an array exported to ExpressJS lib/router/methods.js. This allows me to write the server code as expected. When using queries express.methodOverride()and a POSTwith _method=fooit works. But the actual request FOOdoes not work. As soon as the client sends the first line of the request, the connection is closed by the server:

$telnet localhost 3000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
FOO / HTTP/1.1
Connection closed by foreign host.

I would like to be able to implement this using ExpressJS and without having to hack its main file.

, ?

+5
4

: , . HTTP-.

, HTTP- barebone...

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

( ) telnet GET FOO...

$ 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.

$ 

node

GET

... FOO. , node HTTP-, Express, .

+4

Node HTTP- C.

, HTTP node.


, PURGE, v0.7.5.

+1

, HTTP Node.js ' . Ben Noordius Parsley , . ( .)

, , . , , - . v0.10.x Node.js, .

, , Content-Length Icecast:

SOURCE /live ICE/1.0

:

server.on('connection', function (socket) {
    var originalOnDataFunction = socket.ondata;
    var newLineOffset;
    var receiveBuffer = new Buffer(0);
    socket.ondata = function (d, start, end) {
        receiveBuffer = Buffer.concat([receiveBuffer, d.slice(start, end)]);
        if ((newLineOffset = receiveBuffer.toString('ascii').indexOf('\n')) > -1) {
            var firstLineParts = receiveBuffer.slice(0, newLineOffset).toString().split(' ');
            firstLineParts[0] = firstLineParts[0].replace(/^SOURCE$/ig, 'PUT');
            firstLineParts[2] = firstLineParts[2].replace(/^ICE\//ig, 'HTTP/');
            receiveBuffer = Buffer.concat([
                new Buffer(
                    firstLineParts.join(' ') + '\r\n' + 
                    'Content-Length: 9007199254740992\r\n'
                ), 
                receiveBuffer.slice(newLineOffset +1)
            ]);

            socket.ondata = originalOnDataFunction;
            socket.ondata.apply(this, [receiveBuffer, 0, receiveBuffer.length]);
        }
    };
}

, . , , HTTP , .

0

? HTTP- , . HTTP- ?

To answer the question of whether this hack will require Express, whether it will be necessary to dig into it. Express requiremodule http, and you will need to override this on your own, at least. Node.js inline core modules are always checked first , so you cannot just give your module the same name for this.

-1
source

All Articles