Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    [Hỏi] Nếu thay đổi url request trong 1 middleware nào đó thì các middleware khác không được nhảy đến

    ExpressJS/Conect.IO
    0
    5
    410
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      Chuyển Phạm Ngọc last edited by Vũ

      Chào các bạn mình có 1 vấn đề sau:

      Khi mình thay đổi url của request trong 1 middleware nào đó thì chỉ có middleware ngay sau đó được gọi mà các middleware khác bị bỏ qua. Ví dụ đoạn code sau:

      const express = require('express')
      const app = express()
      
      app.get('/',function (req, res, next) {
       req.url = req.protocol + '://' + req.get('host') + req.originalUrl + 'next'   // thay url bằng http://localhost:3000/nxt
       console.log('middleware1');
       next()
      })
      
      app.use('/nxt',function (req, res, next) {
       console.log('middleware2');
       next()
      })
      
      app.use('/nxt',function (req, res, next) {
       console.log('middleware3');
       next()
      })
      
      app.listen(3000);
      

      Khi mình vào bằng đường dẫn http://localhost:3000 thì phần console chỉ in ra middleware1, middleware2 mà ko in ra middleware3, tức là nó đã không nhảy vào middleware3. Nhưng nếu mình vào bằng http://localhost:3000/next thì phần console in ra middleware2, middleware3, tức là nó đã có chạy vào middleware3. Ai có thể giải thích giúp mình hiện tượng đó được không ạ?

      Cảm ơn các bạn!!

      alt text

      Q 1 Reply Last reply Reply Quote 0
      • Q
        Quy Tran @Chuyển Phạm Ngọc last edited by

        @Chuyển-Phạm-Ngọc Bạn làm theo cách này xem https://gist.github.com/ramonfritsch/06893c1c561d670687a9aee3bbc4e9c7

        Vũ 1 Reply Last reply Reply Quote 0
        • Vũ
          Vũ Global Moderator @Quy Tran last edited by

          @Quy-Tran Bạn có thể giải thích cụ thể hơn không? Tức là với middleware trên 1 route giống nhau thì đưa vào Router à?

          Tech-nông
          Email: [email protected]
          Profile: about.me/vunb
          Github: github.com/vunb
          Twitter: twitter.com/nhubaovu

          Q 1 Reply Last reply Reply Quote 0
          • Q
            Quy Tran @Vũ last edited by

            @Vũ Không cần dùng router

            
            app.get('/default-page', function (req, res) {
              let content = JSON.stringify(config);
            
              res.send('default page');
            });
            
            app.use(function (req, res, next) {
              req.url = '/default-page';
              app.handle(req, res, next); // <--- here
            });
            
            

            Quan trọng là 2 dòng

            req.url = '/default-page';
            app.handle(req, res, next);
            
            1 Reply Last reply Reply Quote 1
            • Sa Tóc Bạc
              Sa Tóc Bạc last edited by

              @Quy-Tran bạn ấy muốn giải thích hiện tượng mà bạn @@"

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              $(document).ready(function () { app.coldLoad(); }); }