यहाँ एक REST API वेब सेवा गतिशील सर्वर के लिए भेजा यूआरएल के आधार पर हैंडलर js फाइल लोड का एक उदाहरण है: js
module.exports =
{
index: function()
{
console.log('index method');
},
close: function()
{
console.log('close method');
}
};
इस कोड को एक छोटा सा आप जादू कर सकते हैं dynamizing :
server.js
var http = require("http");
var url = require("url");
function start(port, route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Server:OnRequest() Request for " + pathname + " received.");
route(pathname, request, response);
}
http.createServer(onRequest).listen(port);
console.log("Server:Start() Server has started.");
}
exports.start = start;
router.js
function route(pathname, req, res) {
console.log("router:route() About to route a request for " + pathname);
try {
//dynamically load the js file base on the url path
var handler = require("." + pathname);
console.log("router:route() selected handler: " + handler);
//make sure we got a correct instantiation of the module
if (typeof handler["post"] === 'function') {
//route to the right method in the module based on the HTTP action
if(req.method.toLowerCase() == 'get') {
handler["get"](req, res);
} else if (req.method.toLowerCase() == 'post') {
handler["post"](req, res);
} else if (req.method.toLowerCase() == 'put') {
handler["put"](req, res);
} else if (req.method.toLowerCase() == 'delete') {
handler["delete"](req, res);
}
console.log("router:route() routed successfully");
return;
}
} catch(err) {
console.log("router:route() exception instantiating handler: " + err);
}
console.log("router:route() No request handler found for " + pathname);
res.writeHead(404, {"Content-Type": "text/plain"});
res.write("404 Not found");
res.end();
}
exports.route = route;
index.js
var server = require("./server");
var router = require("./router");
server.start(8080, router.route);
मेरे मामले में संचालकों एक सबफ़ोल्डर/TrainerCentral में हैं, इसलिए मानचित्रण इस तरह काम करता है:
स्थानीय होस्ट: 8080/TrainerCentral/पकाने की विधि js फ़ाइल को मैप होगा/TrainerCentral /Recipe.js स्थानीय होस्ट: 8080/TrainerCentral/कसरत js फ़ाइल /TrainerCentral/Workout.js करने के लिए नक्शे होगा
यहाँ एक उदाहरण हैंडलर कि, पुन: प्राप्त करने डालने, अद्यतन करने के लिए 4 मुख्य HTTP कार्यों में से प्रत्येक को संभाल सकता है, और डेटा हटाना
/TrainerCentral/Workout.js
function respond(res, code, text) {
res.writeHead(code, { "Content-Type": "text/plain" });
res.write(text);
res.end();
}
module.exports = {
get: function(req, res) {
console.log("Workout:get() starting");
respond(res, 200, "{ 'id': '123945', 'name': 'Upright Rows', 'weight':'125lbs' }");
},
post: function(request, res) {
console.log("Workout:post() starting");
respond(res, 200, "inserted ok");
},
put: function(request, res) {
console.log("Workout:put() starting");
respond(res, 200, "updated ok");
},
delete: function(request, res) {
console.log("Workout:delete() starting");
respond(res, 200, "deleted ok");
}
};
"नोड index.js"
मज़े के साथ कमांड लाइन से सर्वर प्रारंभ
!
मुझे यकीन नहीं है कि मैं समझता हूं। आप बस 'var m = आवश्यकता (' ./ lib/api/user_getDetails.js ') करते हैं और उस प्रतिक्रिया का उपयोग अपनी प्रतिक्रिया में करते हैं। क्या मैं कुछ भूल रहा हूँ? – freakish
मैं इसे गतिशील रूप से लोड करना चाहता हूं। यही है, अगर मैं एक नया एपीआई फ़ंक्शन जोड़ता हूं, तो मुझे मैन्युअल रूप से इसकी आवश्यकता नहीं होती है। लेकिन मुझे यकीन नहीं है कि इसे कैसे पूरा किया जाए। –
तो, आप एक ऑटोलोडर चाहते हैं? –