2014-08-27 18 views
5

Tôi muốn tạo định tuyến tự động trong nhanh, hiện tôi có thể đọc thư mục và thêm đường bằng tay từ tất cả các file có sẵn, thêm tuyến đường cũng có thể được cập nhật nếu có sự thay đổi trong tập tin tuyến đườngCách thêm tuyến tốc hành nếu ứng dụng đã nghe?

delete require.cache[require.resolve(scriptpath)]; 
var routescript = {}; 
try { 
    routescript = require(scriptpath); 
} catch (e){ 
    console.log('Express >> Ignoring error route: ' + route + ' ~ >' + scriptpath); 
} 
var stack_index = app._router.stack_map[route] 
var stack = app._router.stack[stack_index]; 
if (stack) { 
    app._router.stack[stack_index].handle = routescript; 
    console.log('Replace Route Stack \'' + route + '\''); 
} else { 
    app.use(route, routescript); 
    var stack_index = app._router.stack_map[route] = (app._router.stack.length-1); 
    console.log('Add Route Stack \'' + route + '\''); 
} 

Nhưng những người chỉ làm việc chỉ trước khi ứng dụng nghe cổng,

Làm cách nào để thêm/xóa chồng đường mới sau khi ứng dụng nghe cổng?

Một cách tôi có thể nghĩ là đóng configure server/thêm/xóa con đường tái lắng nghe, nhưng tôi đoán đó là một thực tế xấu

+0

Bạn đang thêm loại tuyến đường nào? – Vinz243

+0

có thể nhận, đăng, đặt hoặc xóa – DeckyFx

+0

và phiên bản Express nào? – Vinz243

Trả lời

1

Tôi rất ngu ngốc ....

Express 4 mặc định là có khả năng để thêm đường ngay cả sau khi nó nghe

Vậy tại sao tôi không thể làm điều đó trước đây? bởi vì trên tầng ngăn xếp của bộ định tuyến, tôi thêm lỗi xử lý lớp ngăn xếp, vì vậy bất kỳ lớp bộ định tuyến nào tôi thêm sau đó, sẽ không thể truy cập được theo yêu cầu, bởi vì khi yêu cầu được xử lý, nó sẽ bị lỗi xử lý đầu tiên.

nên phương pháp chính xác là như sau:

  1. tôi phải quản lý những gì index là xử lý lỗi ngăn xếp lớp nằm ở app._router.stack, trong trường hợp này nó là một số lớp tại rất cuối mảng

  2. Thêm tuyến đường mới, ví dụ: sử dụng app.use("/something", function(req, res, next){ res.send("Lol") })

  3. Tháo ngăn xếp lớp xử lý lỗi, và đặt nó ở phút cuối của router ngăn xếp mảng

    // in this case, error map is array // contain index location of error handling stack layer var error_handlers = app._router.stack.splice(error_map[0], error_map.length); app._router.stack.push.apply(app._router.stack, error_handlers);

Bây giờ bạn đã sẵn sàng để đi.

1

Sau khi chọc xung quanh với mã nhanh, tôi thấy điều này:

router.get('/', function(req, res) { 
    res.render('index', { 
    title: 'Express' 
    }); 
    console.log("adding route") 

    addGet('/mypath', function(req, res) { 
    res.send('hi') 
    }); 
}); 

function addGet(path, callback) { 
    Router = require('express').router; 
    // We get a layer sample so we can instatiate one after 
    layerSample = router.stack[0]; 

    // get constructors 
    Layer = layerSample.constructor; 
    Route = layerSample.route.constructor; 

    // see https://github.com/strongloop/express/blob/4.x/lib/router/index.js#L457 
    route = new Route(path); 

    var layer = new Layer(path, { 
    sensitive: this.caseSensitive, 
    strict: this.strict, 
    end: true 
    }, route.dispatch.bind(route)); 
    layer.route = route; 

    // And we bind get 
    route.get(callback) 

    // And we map it 
    router.stack.push(layer); 
} 

Sau đó, mở trình duyệt của bạn tại localhost và sau đó tại localhost/mypath nó hoạt động !!!

Các vấn đề liên quan