Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an example of a REST API web service that dynamically loads the handler js file based on the url sent to the server:</p> <p>server.js</p> <pre><code>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; </code></pre> <p>router.js</p> <pre><code>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; </code></pre> <p>index.js</p> <pre><code>var server = require("./server"); var router = require("./router"); server.start(8080, router.route); </code></pre> <p>handlers in my case are in a subfolder /TrainerCentral, so the mapping works like this:</p> <p>localhost:8080/TrainerCentral/Recipe will map to js file /TrainerCentral/Recipe.js localhost:8080/TrainerCentral/Workout will map to js file /TrainerCentral/Workout.js</p> <p>here is a example handler that can handle each of the 4 main HTTP actions for retrieving, inserting, updating, and deleting data.</p> <p>/TrainerCentral/Workout.js</p> <pre><code>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"); } }; </code></pre> <p>start the server from command line with "node index.js"</p> <p>Have fun!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload