-
-
Notifications
You must be signed in to change notification settings - Fork 22.9k
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (35 loc) · 987 Bytes
/
index.js
File metadata and controls
43 lines (35 loc) · 987 Bytes
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
'use strict'
const express = require('../../')
const app = module.exports = express()
const users = require('./db')
// so either you can deal with different types of formatting
// for expected response in index.js
app.get('/', (req, res) => {
res.format({
html: function () {
res.send('<ul>' + users.map((user) => '<li>' + user.name + '</li>').join('') + '</ul>')
},
text: function () {
res.send(users.map((user) => ' - ' + user.name + '\n').join(''))
},
json: function () {
res.json(users)
}
})
})
// or you could write a tiny middleware like
// this to add a layer of abstraction
// and make things a bit more declarative:
function format (path) {
// eslint-disable-next-line global-require
const obj = require(path)
return function (req, res) {
res.format(obj)
}
}
app.get('/users', format('./users'))
/* istanbul ignore next */
if (!module.parent) {
app.listen(3000)
console.log('Express started on port 3000')
}