Dưới đây là một ví dụ về một dịch vụ web API REST rằng động tải tập tin handler js dựa trên url gửi đến máy chủ :
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);
xử lý trong trường hợp của tôi đang ở trong một thư mục con/TrainerCentral, vì vậy việc lập bản đồ các công trình như thế này:
localhost: 8080/TrainerCentral/Recipe sẽ ánh xạ tập tin js/TrainerCentral /Recipe.js localhost: 8080/TrainerCentral/Workout sẽ ánh xạ tới tệp js /TrainerCentral/Workout.js
đây là trình xử lý ví dụ có thể xử lý từng hành động HTTP chính để truy xuất, chèn, cập nhật và xóa dữ liệu.
/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");
}
};
khởi động server từ dòng lệnh với "index.js nút"
Have fun!
Tôi không chắc mình hiểu. Bạn chỉ cần thực hiện 'var m = require ('./ lib/api/user_getDetails.js')' và sử dụng mô đun đó trong câu trả lời của bạn. Tui bỏ lỡ điều gì vậy? – freakish
Tôi muốn tải động. Tức là, nếu tôi thêm một hàm API mới, tôi không phải tự yêu cầu nó. Nhưng tôi không chắc chắn làm thế nào để thực hiện điều này. –
Vì vậy, bạn muốn có một bộ nạp tự động? –