Untill yester day, It works very well. however, at this morning I run this project by using npm start, It doesn't work.
The code is below:
router.get('/manage', (req, res) => {
if (res.data.manager != 1) { res.redirect('/'); return; }
res.db.getTeamUrlPairs((teams) => {
res.render("manageTeam", {
teams: teams ,
data: res.data
})
})
})
When I connect to /manage, it just redirect to '/' although res.data.manager was 1. So, I tried to change the circumstance such as routing url, inner code. But, I changed it to:
router.get('/manage', (req, res) => {
res.send('hello');
})
It doesn't work. I mean, the change not reflected. I've restarted my computer, close my vscode, stop and re-run npm. it always just redirect to root.
What can I do for this?
Ps. Other routings are working well... what the hell is going on.
CodePudding user response:
Routing order is important.
router.get('/:a',(req,res)=>{})
router.get('/manage', (req,res)=>{})
This code won't work well. specifically manage doesn't work. However, below:
router.get('/manage', (req,res)=>{})
router.get('/:a',(req,res)=>{})
does work well.
CodePudding user response:
Note the differences between app.get()
and app.use()
methods:
app.get(<registered_path>, <middleware>)
is triggered only on GET requests that have an exact match to the registered_path.
app.use(<registered_path>, <middleware>)
is triggered for all HTTP methods (POST, GET, PUT, PATCH, and DELETE) where the URL path of the request start with the registered_path.
The order in which you specify your middlewares is important. If we registred the following middlewars:
app.use('/', <middleware1>)
and
app.get('/manage', <middleware2>)
The request to /manage will never trigger middleware2 (unless you called the next()
function in middleware1
).
Also, consider using breakpoints in each of your middlewares to see what middleware is actually triggered when you send an HTTP request to /manage.