[JS] [Node.js] Express error on windows evirement

說明

'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'

The issue is that request.url doens't contain the hash, because the
browser typically doesn't send the hash, so the server doesn't have
it.


瀏覽器送給server的request.url不會包含hash

討論串範例

can't get the hash of a url

I'm trying to get the hash of a url that's being passed in to my
simple http server, but the only params that appear to be in the uri
are the path and href. The following is my code:

var http = require('http'),
    url = require('url'),
    path = require('path');

http.createServer( function(request, response) {

    var uri = url.parse(request.url);

console.log( uri.hash );

    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end( uri.href );

}).listen(8080);

On the terminal, no uri.hash is being output, just undefined.

Re: Re: can't get the hash of a url

On Fri, Aug 13, 2010 at 22:30, markc <mconstable@...> wrote:
> That probably is true in general but php is nice enough to provide the
> hash fragment...

Node is even nicer.

node> require("url").parse('http://u:pw-hcDgGtZH8xNBDgjK7y7TUQ <at> public.gmane.org/path?arg=1#anchor')
{ href: 'http://u:pw-hcDgGtZH8xNBDgjK7y7TUQ <at> public.gmane.org/path?arg=1#anchor'
, protocol: 'http:'
, slashes: true
, host: 'u:
pw@...'
, auth: 'u:pw'
, hostname: 'example.com'
, hash: '#anchor'
, search: '?arg=1'
, query: 'arg=1'
, pathname: '/path'
}

The issue is that request.url doens't contain the hash, because the
browser typically doesn't send the hash, so the server doesn't have
it, and can't put it on request.url.

On Fri, Aug 13, 2010 at 21:50, Carl S. Yestrau Jr.
<
carl@...> wrote:
> The fragment identifier functions a bit different than the rest of the
> URI. To my understanding its processing is exclusively client-side
> with no involvement with the server. That being said, I've seen it in
> my nginx access logs.

According to the spec, the client MUST send the path portion, but MAY
send the protocol, hostname, and hash.  If there is a host header,
then the hostname in the request url MUST match the host header.

Browser behavior is convention only.  If node gets the hash, and/or
the hostname, then it will provide it on the request.url.


Reference

http://comments.gmane.org/gmane.comp.lang.javascript.nodejs/10812
http://nodejs.org/api/url.html

留言

這個網誌中的熱門文章

Mac起步:常用軟體及設定

台灣可用線上音樂串流網站

醍醐味