使用ExpressJS提供非标准HTTP方法

使用ExpressJS提供非标准HTTP方法

本文介绍了使用ExpressJS提供非标准HTTP方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个使用非标准HTTP方法(动词)回答请求的HTTP服务器。例如,客户端将发出如 FOO / HTTP / .1.1 的请求。而在服务器端,这个请求将被处理如下:

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

我将非标准方法附加到ExpressJS的 lib /路由器/ methods.js 。这样我可以按预期的方式写我的服务器代码。当使用 express.methodOverride() POST 请求与 _method = foo ,它工作。但实际的 FOO 请求无效。一旦客户端发送请求的第一行,连接将被服务器关闭:

  $ telnet localhost 3000 
尝试127.0.0.1 ...
连接到本地主机。
转义字符为'^]。
FOO / HTTP / 1.1
外部主机关闭连接。

我希望能够使用ExpressJS实现此功能,而无需避免黑客入侵其核心文件。 / p>

任何想法,如果这是可能的,如何?

解决方案

:不,不可能。不需要实现自己的HTTP模块。



要测试,启动一个准系统HTTP服务器...

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

然后(如您已经完成)telnet到它并发出GET和FOO请求.. 。

  $ telnet localhost 8080 
Trying :: 1 ...
telnet:连接到地址: :1:连接拒绝
尝试127.0.0.1 ...
连接到本地主机。
转义字符为'^]。
GET / HTTP / 1.1

HTTP / 1.1 200 OK
连接:keep-alive
传输编码:chunked

0

FOO / HTTP / 1.1
外部主机关闭连接。

$

在节点控制台中,您将看到

  GET 

。 ..但没有FOO。因此,Express使用的节点的本机HTTP模块不会使这些请求可用。


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. And on the server side, this request would be handled by 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 appended my non-standard method to the array exported in ExpressJS's lib/router/methods.js. This allow me to write my server code as expected. When using express.methodOverride() and a POST request with _method=foo, it works. But an actual FOO request doesn't work. As soon as the client send 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 with ExpressJS and without avoid hacking into its core file.

Any idea if this is possible and how?

解决方案

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.

这篇关于使用ExpressJS提供非标准HTTP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:32