-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path59_negotiator.js
35 lines (32 loc) · 1.02 KB
/
59_negotiator.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
const Negotiator = require('negotiator');
const http = require('http');
const tiposDisponibles =
['text/html',
'application/xml',
'application/json'
];
const alumnos = require('./alumnos');
http.createServer(showAlumnos).listen(3000);
function showAlumnos(req,resp) {
const negotiator = new Negotiator(req);
const mediaType = negotiator.mediaType(tiposDisponibles);
switch (mediaType) {
case 'application/xml':
resp.setHeader('content-type',mediaType);
console.log("XML...")
resp.end(alumnos.toXML());
break;
case 'application/json':
resp.setHeader('content-type',mediaType);
console.log("JSON...")
resp.end(alumnos.toJson());
break;
case 'text/html':
resp.setHeader('content-type', mediaType);
resp.end(alumnos.toHTML());
break;
default:
resp.setHeader('content-type', 'text/html');
resp.end(alumnos.toHTML());
}
}