S
S
sozercanie_kosmosa2014-11-03 20:38:35
JavaScript
sozercanie_kosmosa, 2014-11-03 20:38:35

How to give the browser a JavaScript file?

In response to a request:

Remote Address:127.0.0.1:80
Request URL:http://localhost/js/jquery.js
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,ru;q=0.6,de;q=0.4
Connection:keep-alive
Host:localhost
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Response Headersview source
Connection:keep-alive
Content-Type:text/javascript
Date:Mon, 03 Nov 2014 16:17:10 GMT
Transfer-Encoding:chunked

Chrome browser 38.0.2125.111 m (Win 8)
when loading a page from Node.js v0.10.33 server :
<!doctype html>
<html>
<head>
  <script src="js/jquery.js"></script>
  ...

the server tries to serve the script(js/jquery.js)
like this:
path = './js/jquery.js';

var stream = fs.createReadStream(path, 'utf8');
stream.on('readable', function () {

    response.writeHead(200, {'Content-Type': 'text/javascript'});

    var out;
    while ((out = stream.read()) !== null)
        response.write(out, 'utf8');
});

stream.once('end', function () {
    response.end();
});

to which an error occurs in the browser console:
GET localhost/js/jquery.js net::ERR_INVALID_CHUNKED_ENCODING

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Popov, 2014-11-03
@sozercanie_kosmosa

https://gist.github.com/djphoenix/0a18cc0b17d067358e8f

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question