Add hostname route to middleware#308
Merged
Merged
Conversation
bimmlerd
approved these changes
Apr 27, 2026
bimmlerd
reviewed
Apr 27, 2026
|
|
||
| if (req.path.indexOf("/hostname") === 0) { | ||
| res.json({ hostname: os.hostname() }) | ||
| return |
Member
There was a problem hiding this comment.
nit: technically this I guess should not return but also fall through to next
Member
Author
There was a problem hiding this comment.
Yeah, I guess either approach should work as we don't expect any further operation afterwards, but omitting the return statement looks indeed more consistent with the other occurrences. Dropping.
Member
Author
There was a problem hiding this comment.
I spoke too early actually, as omitting the return statement causes the server to emit a log error for every request along the lines of:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (node:_http_outgoing:642:11)
at ServerResponse.header (/usr/local/lib/node_modules/json-server/node_modules/express/lib/response.js:794:10)
at ServerResponse.send (/usr/local/lib/node_modules/json-server/node_modules/express/lib/response.js:174:12)
at ServerResponse.jsonp (/usr/local/lib/node_modules/json-server/node_modules/express/lib/response.js:351:15)
at router.render (/usr/local/lib/node_modules/json-server/lib/server/router/index.js:46:9)
at /usr/local/lib/node_modules/json-server/lib/server/router/index.js:84:12
at Layer.handle [as handle_request] (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:328:13)
at /usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:286:9
at router.process_params (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:346:12)
I'll go ahead and restore the return statement, and add it to the client-ip route as well.
88003cb to
7acaa2d
Compare
It returns the hostname of the server. For instance:
$ curl --silent localhost:80/hostname
{
"hostname": "foo"
}
Signed-off-by: Marco Iorio <marco.iorio@isovalent.com>
Otherwise, the downstream logic then tries to set the headers
after that the data has already been sent to the client, causing
log errors along the lines of:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (node:_http_outgoing:642:11)
at ServerResponse.header (/usr/local/lib/node_modules/json-server/node_modules/express/lib/response.js:794:10)
at ServerResponse.send (/usr/local/lib/node_modules/json-server/node_modules/express/lib/response.js:174:12)
at ServerResponse.jsonp (/usr/local/lib/node_modules/json-server/node_modules/express/lib/response.js:351:15)
at router.render (/usr/local/lib/node_modules/json-server/lib/server/router/index.js:46:9)
at /usr/local/lib/node_modules/json-server/lib/server/router/index.js:84:12
at Layer.handle [as handle_request] (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:328:13)
at /usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:286:9
at router.process_params (/usr/local/lib/node_modules/json-server/node_modules/express/lib/router/index.js:346:12)
Signed-off-by: Marco Iorio <marco.iorio@isovalent.com>
7acaa2d to
00dec1a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It returns the hostname of the server. For instance: