forked from Raynos/http-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
35 lines (31 loc) · 1014 Bytes
/
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
// https://github.com/visionmedia/express/blob/master/examples/content-negotiation/index.js
var http = require("http")
var Router = require("routes-router")
var mediaTypes = require("media-types")
var sendText = require("send-data")
var sendJson = require("send-data/json")
var sendHtml = require("send-data/html")
var users = [
{ name: "Tobi" },
{ name: "Loki" },
{ name: "Jane" }
]
var app = Router()
app.addRoute("/", mediaTypes({
"text/html": function (req, res) {
sendHtml(req, res, "<ul>" + users.map(function (user) {
return "<li>" + user.name + "</li>"
}).join("") + "</ul>")
},
"application/json": function (req, res) {
sendJson(req, res, users)
},
"default": function (req, res) {
sendText(req, res, users.map(function (user) {
return " - " + user.name + "\n"
}).join(""))
}
}))
var server = http.createServer(app)
server.listen(3000)
console.log("content negotiation server listening on port 3000")