/
index.js
53 lines (45 loc) · 1.29 KB
/
index.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
/**
* This example shows how to build the routing stack of an Express application dynamically.
*/
'use strict'
if (typeof Promise === 'undefined')
global.Promise = require('bluebird')
var resolve = require('path').resolve,
express = require('express'),
adir = require('../..'),
dir = resolve(__dirname, 'routes'),
app = express()
function onEntry(stats, router) {
if (stats.isDirectory()) {
var subrouter = new express.Router
router.use('/' + stats.basename, subrouter)
return subrouter
}
else
require(stats.path).call(router)
}
function onError(err) {
console.error('cannot build the routing table :(')
console.error(err.stack)
}
function onEnd() {
app.listen(3333, function () {
console.log(
'server is ready to accept connections on port 3333\n\n' +
'try:\n' +
"$ curl 'http://localhost:3333/users/1'\n" +
"$ curl 'http://localhost:3333/posts/1'\n"
)
})
}
function done(err) {
if (err)
onError(err)
else
onEnd()
}
// note:
// `app` is a function, so we have to pass `done` here,
// otherwise `app` itself would be treated as the aggregation callback.
// (Because aggregation initial value is optional.)
adir(dir, onEntry, app, done)