Jump to content
Sign in to follow this  
Daniel Eiszele

NodeJS Route Manager

Recommended Posts

The attached uRouter.pas file is something I've been playing with for the last couple of days. It is basically a route manager (think ExpressJS) for Smart Mobile Studio.  It isn't fully baked at this point in time and I probably won't progress it far past this point as it already meets my needs from a REST Service perspective. Feel free to use it as a base for your own efforts though. It works in both http and https environments.  Just create a global var on your NodeProgram object to hold it and instantiate it after the server object is created with the following call:

FRouter := TRouteManager.Create(FServer);

Do not manually set the request handler as the constructor sets that up for you.

To create routes, use the following syntax:

  FRouter.get('/', procedure (const Request: TNJHttpRequest;
    const Response: TNJHttpResponse; data : TRequestData; data : TRequestData)
    begin
      Response.end('You Selected the Root Directory');
    end);

  FRouter.post('/', procedure (const Request: TNJHttpRequest;
    const Response: TNJHttpResponse; data : TRequestData; data : TRequestData)
    begin
      Response.end('You Selected a post request');
    end);

  FRouter.put('/', procedure (const Request: TNJHttpRequest;
    const Response: TNJHttpResponse; data : TRequestData; data : TRequestData)
    begin
      Response.end('You Selected a put request');
    end);

  FRouter.delete('/', procedure (const Request: TNJHttpRequest;
    const Response: TNJHttpResponse; data : TRequestData; data : TRequestData)
    begin
      Response.end('You Selected a delete request');
    end);

To access data when it's available, you may manually retrieve it from the request object or use one of two functions on the data / TRequestdata object as per below.

      data.BodyAsBuffer(procedure (_Buffer : JNodeBuffer)
      begin
        Response.End('Here is the buffer: ' + _Buffer.toString('utf8');
      end);

or

      data.BodyToFile('C:\temp\aFile.pdf',procedure(err : JError)
      begin
        if assigned(err) then
        begin
          Response.end('Something went wrong');
        end
        else
        begin
          Response.end('File has been saved');
        end
      end);

Similarly, url parameters can be read off the TRequestdata object as follows.

Response.end('You requested id: ' + data.Params.ReadString('id'));

Let me know what you think :)

uRouter.pas

Share this post


Link to post
Share on other sites

I am an engineering surveyor, currently working in an Electrical Design office providing "As Built" information to the Civil Construction Industry. The current iteration of this particular program is as an in house REST server, receiving mapping data (in SVG format) for inclusion in a POSTGRES database for later viewing in the browser. In a nutshell, it is a way for our office staff to tell the field staff what they need to do! The front end (my main application), is a hybrid Survey/Cad package written in Delphi; so all this web stuff is quite new to me.  Having said that, I think I have all the pieces now to extend the backend significantly and provide functionality I didn't originally plan on.  NodeJS really is a great platform and being able to access it with object pascal is icing on the cake.

I'll upload my wrappers for POSTGRES and user Authentication too when I get them cleaned up a bit, as they may also be useful as a starting point for others.  Note that coding is not my day job, but a means to an end, so the code and documentation may not (please read "will not" :)) be great.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×