-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.js
45 lines (33 loc) · 936 Bytes
/
5.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
/**
*Simple Express app
*with multiple middleware functions.
*/
// create an express app
var express = require('express'),
morgan = require('morgan'),
app = express(),
port = process.env.PORT || 3000,
testMode = false;
// add logging middleware to log each request
// see: https://www.npmjs.org/package/morgan
app.use(morgan('dev'));
// add middleware to always send a 'hello world' response
app.use(function(req, res, next) {
if (req.url == '/test') {
console.log('enabling test mode');
testMode = true;
}
next();
});
// add middleware to always send a 'hello world' response
app.use(function(req, res) {
var data = testMode ? JSON.stringify(req.headers) : '<h1>hello world</h1>';
var contentType = testMode ? 'text/plain' : 'text/html';
res.writeHead(200, {
'Content-Type': contentType
});
res.end(data);
});
//listen on localhost:3000
app.listen(port);
console.log('server started on port %s', port);