-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver1.js
51 lines (35 loc) · 1.83 KB
/
server1.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
const App = require('./Core/App');
const Router = require('./Core/Router');
const app = new App();
const router = new Router();
router.get('/routerUri1', (req, res) => {res.json('routerUri1')})
app.get('/uri1', (req, res) => { console.log('handler1'); res.json('handler1') })
app.get('/uri2', (req, res) => { console.log('handler2'); res.json('handler2') })
app.get('/uri1', (req, res, next) => { console.log('middleware executed'); next(); }, (req, res) => { console.log('handler3'); res.json('handler3') })
app.post('/postUri1', (req, res) => { res.json('postUri1') })
const middleware1 = (req, res, next) => { console.log('middleware1'); next() }
const middleware2 = (req, res, next) => { console.log('middleware2'); next() }
const middleware3 = (req, res, next) => { console.log('middleware3'); next() }
// app.use(middleware1, middleware2, middleware3)
app.get('/uri3', (req, res, next) => {console.log('uri3 middleware'); next();}, (req, res) => { console.log('uri3'); res.json('uri3') })
// app.use('/uri4', middleware1, (req, res) => {console.log('uri 4'); res.json('uri4 response')})
// app.use('/uri5', (req, res, next) => {console.log(1); next()}, (req, res, next) => {console.log(2); next()}, (req, res, next) => {console.log(3); next()}, router)
const router2 = new Router();
router2.get('/router2', middleware1, middleware3, (req, res) => res.json('router2'))
app.use('/uri6', middleware1, middleware2, router2);
// console.log(app.applicationRoutes)
// console.log(app.applicationRoutes[app.applicationRoutes.length-10])
/*
we can use
**********
app.use(middlewares)
app.use(uri, router)
app.use(uri, middlewares, router)
app.use(uri, controller)
----router
router.use(middlewares)
router.get(uri, controller)
router.get(uri, middlewares, controller)
*/
const PORT = 5000;
app.listen(5000, () => console.log('Server is listening on port', PORT));