我想编写一个 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/router/methods.js
。这使我能够按预期编写服务器代码。使用时express.methodOverride()
and a POST
请求与_method=foo
, 有用。但实际的FOO
请求不起作用。一旦客户端发送请求的第一行,服务器就会关闭连接:
$telnet localhost 3000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
FOO / HTTP/1.1
Connection closed by foreign host.
我希望能够使用 ExpressJS 来实现这一点,并且避免侵入其核心文件。
知道这是否可能以及如何实现吗?
简短的回答:不,这是不可能的。除非实现您自己的 HTTP 模块。
要进行测试,请启动准系统 HTTP 服务器...
$ 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.
$
在节点控制台中你会看到
GET
...但没有FOO。因此,Express 使用的 Node 的本机 HTTP 模块不会使这些请求可用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)