使用快遞,你可以添加路由服務器正在偵聽之後,所以它不是一個問題。注意優先級,因爲它將被添加到堆棧的底部。
運行的節點上的應用程序加載所有以dB爲單位的路由(前聽)時,我會建議有一個分貝存儲路線,並將其添加到路由器。爲了能夠擴展您的應用程序,並能夠安全地重新啓動它。
然後開始聽,並有一個路由添加路由,刪除路由,路由更新等
這裏是收聽之後添加路由的一個簡單的例子:
const app = require('express')();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const someGenericHandler = function(req, res) {
return res.json({ message: 'foobar' });
};
// it creates a route
app.post('/routes', function(req, res) {
try {
const route = req.body;
app[route.method](route.path, someGenericHandler);
return res.json({ message: `route '${route.method}${route.path}' added` });
} catch(err) {
return res.status(500).json({ message: err.message || 'an error occured while adding the route' });
}
});
app.listen(process.env.PORT);
你可以試試這個代碼,將其粘貼到一個文件中,讓我們說index.js。 運行npm i express body-parser
,然後PORT=8080 node index.js
,然後發送POST請求http:/localhost:8080/routes
用JSON有效載荷(和正確的Content-Type頭,使用郵遞員) 這樣的:{ method: 'get', path:'/' }
,然後嘗試用GET請求@「
您全新的路線
請注意,如果您期望擁有每分鐘幾百請求的用戶千萬,我會強烈建議讓每個用戶一個單一的應用程序和用戶註冊主應用程序,也許產卵每個應用小VPS一些自動化腳本當用戶註冊時,或者對每個用戶有某種請求限制。
希望這會有幫助
非常感謝François,這絕對有幫助,我想知道如果這樣的事情是可能的。我會考慮你的意見。再次感謝。 – mamsoudi