Lon*_*ist 4 javascript backend node.js express server
这两种说法有什么区别:
app.get('/',someFunction);
app.route('/').get(someFunction);
Run Code Online (Sandbox Code Playgroud)
请注意,我不是在比较 router.get 和 app.get
假设你想在同一条路径上做三个路由:
app.get('/calendarEvent', (req, res) => { ... });
app.post('/calendarEvent', (req, res) => { ... });
app.put('/calendarEvent', (req, res) => { ... });
Run Code Online (Sandbox Code Playgroud)
这样做需要您每次都复制路线路径。
你可以这样做:
app.route('/calendarEvent')
.get((req, res) => { ... })
.post((req, res) => { ... })
.put((req, res) => { ... });
Run Code Online (Sandbox Code Playgroud)
如果您在同一路径上有多个不同动词的路由,那么它基本上只是一条捷径。我从来没有机会使用它,但显然有人认为它会很方便。
如果您有某种通用中间件,并且仅适用于这三个路由,那么它可能会更有用:
app.route('/calendarEvent')
.all((req, res, next) => { ... next(); })
.get((req, res) => { ... })
.post((req, res) => { ... })
.put((req, res) => { ... });
Run Code Online (Sandbox Code Playgroud)
也可以将新的路由器对象用于类似目的。
而且,我想如果我不解释这两个陈述之间没有区别(这是您所问的部分),我想我会失职:
app.get('/',someFunction);
app.route('/').get(someFunction);
Run Code Online (Sandbox Code Playgroud)
他们做的事情完全一样。我的其余答案是关于您还可以使用第二个选项做什么。
归档时间: |
|
查看次数: |
727 次 |
最近记录: |