-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (36 loc) · 1.02 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
const App = require('./Core/App');
const app = new App();
const userRouter = require('./router')
const PORT = 5000;
const mw1 = (req, res, next) => {
console.log('mw1')
next()
}
const mw2 = (req, res, next) => {
console.log('mw2')
next()
}
const mw3 = (req, res, next) => {
console.log('mw3')
next()
}
// application level middleware
app.use(mw1, mw2, mw3)
// app.get(uri, controller)
app.get('/uri1', (req, res) => {res.json('uri1')})
// app.get(uri, middlewares, controller)
app.use('/uri2', mw3, mw2, mw1, (req, res) => {res.json('uri3')})
// app.use(uri, middlewares, router)
app.use('/user', mw3, mw2, mw1, userRouter)
// route parameters can be accessed by req.parans
app.get('/user/:id', (req, res) => {
console.log(req.params.id)
res.json('req.params')
})
app.listen(PORT, () => console.log('server is listening on', PORT));
// get: [
// [ [Function: mw1], [Function: mw2], [Function: mw3] ],
// { '/uri1': [Object] },
// { '/uri2': [Object] },
// { '/user': [Object] }
// ],