-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (55 loc) · 1.85 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// MacBook Pro
// const lib = '/Users/alund/Music/iTunes/iTunes Music Library.xml'; // Yes just use " " - space, no "/".
// iMac
const lib = '/Users/lund/Music/Music/Media.localized/Library.xml';
const fs = require('fs');
const http2 = require('http2');
const path = require('path');
const itunes = require('itunes-data');
const parser = itunes.parser();
const readStream = fs.createReadStream(path.resolve(lib));
// console.log(stream); // works.
parser.on('track', function (track) {
// console.log("track:", track);
});
// parser.on('artist', function(artist) {
// console.log("artist:", artist);
// });
// parser.on('album', function(album) {
// console.log("album:", album);
// });
// parser.on('playlist', function(playlist) {
// console.log("playlist:", playlist);
// });
// parser.on('library', function(library) {
// console.log("library:", library);
// });
readStream.pipe(parser); // server starts, but then on page refresh an error.
//
// But how to maintain streaming ?
// By default, it should not throw an event, but it does. Bug?
//
//
// HTTP/2 Server using NodeJS and HTTP2 API
//
// https://nodejs.org/api/http2.html#http2_http_2
// const server = http2.createServer(); // doesn't work
const server = http2.createSecureServer({
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem')
});
server.on('error', (err) => console.error(err));
server.on('stream', (httpStream, headers) => {
// httpStream is a Duplex
// TODO code here
// console.log('READ STREAM', readStream);
// console.log('HTTP STREAM', httpStream);
httpStream.respond({
'content-type': 'text/html',
':status': 200
});
httpStream.end('<h1>Hello World</h1>');
});
server.listen(8443, () => {
console.log('EXPERIMENTAL. https://localhost:8443/ running on port 8443. Run `http-server` instead to get localhost:8080');
});