I'm just playing around building an API_Routing system that will let me build class focused modules for an API, I'm not necessarily aiming for industry best practices and am just fleshing out ideas on how to build things in javascript for curiosity.
I have a ServerManager class that when the server is run handles all of the processing, this class uses the API_Router class I developed to process the URL that was provided and pass the request to the proper module for handling.
Currently the URL I am using is http://localhost:8080/user/add/23123/
/user/ This tells my program that I am intending on accessing the User Module.
Each Module is able to define, both submodules or functions inside of the routeMapping function.
I have am attaching my User.js file that handles the User Module.
var http = require('http');
//#File: UserManager.js
///http://localhost:8080/user/23123/
///http://localhost:8080/user/add/23123/
var UserManager = {};
UserManager.Create = function() {
var UserAcc = {
"Username": "test123",
"FirstName": "cody",
"LastName": "jones",
"Id": "000001"
};
return UserAcc;
}
UserManager.route = function(){
return 'user';
}
UserManager.routeMapping = {
"add": this.Create
}
UserManager.Run = function(Data){
var runMe = Data[0];
console.log(this.routeMapping);
console.log(this.routeMapping[runMe]);
}
UserManager.hasSubmodule = false;
module.exports = UserManager;
I am trying to build the routeMapping function to correlate to all of the functions I build for my API, so I need to ensure that the proper function is called based off the URL.
UserManager.routeMapping = {
"add": this.Create //What am I doing wrong here
}
UserManager.Run = function(Data){
var runMe = Data[0];//This will resolve to a string "add"
this.routeMapping[runMe]();//This should run the function stored in the routemapping
}