Comments (10)
Why was this closed? I'd think this would be a sensible feature. I'd like to see it supported — would even consider contributing it myself, if that'd be desirable.
from express.
it's added where possible, however we can't easily support it everywhere, it (unfortunately) has to be part of application logic as well. For example when streaming a static file you wouldn't want to read the file into memory when you can just stat() on HEAD for the content-length etc
from express.
it's been added to res.send() and some other places
from express.
That sounds promising, but it's pretty confusing that HTTPServer
and HTTPSServer
have get()
and options()
etc. but not head()
. It's also just downright odd that the variable RFC2616
in router/methods.js
is missing HEAD
.
from express.
yeah the issue in my mind is that you wouldn't want to reproduce all the GET logic just for HEAD, so HEAD in the router becomes a GET. I dont see a way without hacking core node to support HEAD natively, and even then in many cases you would perform more computing than necessary. Not an easy issue for node
from express.
if you have any ideas let me know :D I just think this solution is better than an app.head() everywhere with duplicate logic
from express.
TJ, I'm just getting to know Express so I suspect I'm just missing something here, I'm just bringing to the table my understanding of how I'd expect a web app framework to work — to me, sure, HEAD is generally the same as GET (except you don't send the body of course) — but sometimes when implementing an app you do want to programmatically handle HEAD differently than GET. For example if you have a more efficient way to generate the headers than generating the entire response body — which, granted, is rare.
Anyway, I'm also not sure what you mean about hacking core node to support HEAD natively. I would think it'd just work out of the box, and indeed I've coded up a quick example that seems to work correctly:
require('http').createServer(function(req, res) {
var body = 'hello world';
var headers = {'Content-Type': 'text/plain', 'Content-Length': body.length};
if (req.method !== 'HEAD' && req.method !== 'OPTIONS') {
res.writeHead(200, headers);
res.write(body);
} else {
res.writeHead(204, headers);
}
res.end();
}).listen(1337, "127.0.0.1");
$ curl -i -X GET http://127.0.0.1:1337/
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 11
Connection: keep-alive
hello world
$ curl -i -X HEAD http://127.0.0.1:1337/
HTTP/1.1 204 No Content
Content-Type: text/plain
Content-Length: 11
Connection: keep-alive
I did notice that sending a 200 in response to HEAD
caused the connection to stay open, I guess curl saw the Connection: keep-alive
header and was waiting for the response body. Seems like a bug in curl, it should know that no body is coming back in response to HEAD. Anyway, that problem can be fixed/bypassed by either sending 204 or Connection: close
— I tested both.
from express.
The one thing we could do, is possibly support both techniques I mentioned. So we would have app.head(), falling back on to app.get() if nothing is defined via app.head(), because the app.get() calls may be identical but simply ignore the body on HEAD, for example res.render() and res.send() handle this automatically so if you are rendering a template it will just work as-is without defining app.head()
from express.
Sounds great!
Would you like me to see if I can put together a patch for that?
from express.
yeah sure if you want to
from express.
Related Issues (20)
- when NODE_ENV='production' getting error on request send HOT 2
- How to flush a response? HOT 2
- Import syntax error with express.Router() import HOT 4
- How do you guys set the `req.hostname`? HOT 1
- [deleted] HOT 3
- Request triager role for @SarthakParikh HOT 1
- Working On Vite+React. Have an Issue. HOT 1
- 4.19.0 breaks passing new URL() into res.redirect(...) HOT 7
- The npm latest version of express is 5.0.0-beta.2 !!! HOT 10
- Troubleshooting Node.js Express Server Routing Issue HOT 1
- mime.charsets in Response.js is undefined HOT 8
- WebAssembly Out of memory Error for any server HOT 2
- return res.redirect('back'); returns to '/' instead of the previous page HOT 2
- Need a clarification on router.param() usage. HOT 4
- [Question] Should Express.js v5.x drop support to older versions of Node? HOT 3
- .get() attempting to grab from home dir (Linux) HOT 2
- Request triager role for Ivaylo-Iv HOT 1
- Request triager role for etroynov HOT 2
- clean code for switch condition HOT 2
- Response location broken for unicode domains in latest release HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from express.