Reputation: 1976
I am learning node and have put together a working server and framework. After talking with some more experienced developers, it seems that Im doing unnecessary work to route the URI. Evidently the http module has all of those features build in already, although I am not sure what they are or how to apply them.
Here is my current server.js
// SERVER
var http = require('http');
var url = require('url');
var fs = require('fs');
var lib = require('scripts'); // some custom libraries I use
var path = require('path');
var NodeSession = require("node-session");
var _SESSION = new NodeSession({secret: 'Q3UBzdH9GEfiRCTKbi5MTPyChpzXLsTD'});
// uri slug -> controller file
var routes = {
// Dependencies
'/style' : 'control_style', // CSS stylesheets
'/fonts' : 'control_fonts', // Fonts
'/scripts' : 'control_scripts', // JS library and controls
'/public' : 'control_public', // Public Resources
// Page Routes
'/' : 'control_home', // root path
'/login' : 'control_login', // login path
'/logout' : 'control_logout', // logout path
'/admin' : 'control_admin', // admin panel path
'/test' : 'control_test', // test page
'/upload' : 'control_upload', // upload test page
};
// Main Loop
function start(){
var port = process.env.PORT || 8080;
http.createServer(function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
_SESSION.startSession(request, response, function(){
lib._post(request, function(_POST, form, files){ // grabs and parses post data
// file system objects that may be needed in controllers
var _SYSTEM = {fs, form, files, path, pathname};
// get the respective controller module
var module = routes[pathname];
// hack for uri's referencing public directory
if(pathname.includes('/public')) module = routes['/public'];
if(module!=null) { // load the respective controller
console.log("controller: "+module+".js");
var controller = require("../controller/"+module);
controller.data(response, request, _SYSTEM, _POST, _SESSION);
} else { // Not Found
console.log(pathname+' => Not Found');
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Path not found");
response.end();
}
});
});
}).listen(port);
console.log("Server has started.");
}
exports.start = start;
Basically, Im matching each URI to a key in the "routes" object and loading the respective controller. (Normally, I would extend this functionality using Regex, but just keeping it simple for now).
Does the Node "http" module (or any other module) already have built-in the functions to parse URI such that I dont have to use a custom built router?
Please note: I am using native node and trying to avoid Express.js for this particular exercise
Upvotes: 0
Views: 279
Reputation: 111506
Note: This answer was written before this additional requirement was added to the answer: "I am using native node and trying to avoid using Express for this particular exercise"
There are a lot of Node modules that can be used to simplify what you do here.
For example you can use Express:
var app = require('express')();
app.get('/path1', path1Handler);
app.get('/path2', path2Handler);
app.use('/static', express.static(pathToStaticFiles));
app.listen(3333);
etc.
Upvotes: 1