-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.js
56 lines (44 loc) · 1.25 KB
/
11.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
/**
*Simple Express routing example that
*returns different content type responses.
*Can only access the following routes:
*
* /hello1
* /hello2
* /hello3
* /hello4
* /hello5
* /hello6
* Ref: http://expressjs.com/4x/api.html#res.send
*/
var app = require('express')(),
morgan = require('morgan'),
port = process.env.PORT || 3000;
// add logging middleware
app.use(morgan('dev'));
// route handler for GET /
app.get('/hello1', function(req, res) {
res.send('<h1>hello world</h1>'); // automatic -> text/html
});
app.get('/hello2', function(req, res) {
res.header('Content-Type', 'text/plain');
res.send('hello world\n'); // explicit -> text/plain
});
app.get('/hello3', function(req, res) {
res.send(new Buffer('hello world\n')); // automatic -> application/octet-stream
});
app.get('/hello4', function(req, res) {
res.send({
message: 'hello world'
}); // automatic -> application/json
});
app.get('/hello5', function(req, res) {
res.send(['hello world']); // automatic -> application/json
});
app.get('/hello6', function(req, res) {
res.header('Content-Type', 'text/xml');
res.send('<message>hello world</message>'); // explicit -> text/xml;
});
//listen on localhost:3000
app.listen(port);
console.log('server started on port %s', port);