Well, it depends on what extension points the server gives you.  Remember
that node's http request handler is just a callback.

In express this is done using the connect middleware format.  Most web
frameworks already have an extension API where you can add new routes via
HTTP Verb and url pattern.

If you're using a framework, the code you're looking for it probably
already there.  If not, then you'll have to code in the extension points
yourself.

On Thu, Apr 19, 2012 at 10:29 AM, Masiar <[email protected]> wrote:

> Thanks! That's a nice starting point. What I was wondering is: can I
> do this as a node.js extension? Let's say a programmer already has his
> own server created. I would like to extend whatever he did with what
> you wrote. http.createServer won't be my choice since a server already
> exists, right? How can I "add" those matched patterns (with relative
> callbacks for GET/PUT/POST/DELETE) to an already created server?
> Thanks a lot!
>
> On Apr 19, 5:24 pm, Tim Caswell <[email protected]> wrote:
> > Ok, for HTTP request pathname matching you can just use express.  I'm
> > pretty sure it allows this kind of pattern matching our of the box.  To
> > implement such matching yourself it easy.
> >
> > var http = require('http');
> > var url = require('url');
> >
> > // Match /some/folder/:id/*
> > var pattern = new RegExp("^/some/folder/([^/]+)(.*)$");
> >
> > http.createServer(function (req, res) {
> >   // pathname removes trailing query strings and gives just the path
> >   var pathname = url.parse(req.url).pathname;
> >   var match = pathname.match(pattern);
> >   if (match) {
> >     // the request matched
> >     // :id is in match[1] and anything after it is in match[2];
> >   }
> >
> > })
> >
> > What most frameworks will do is take your string like
> > "/foo/bar/:param1/:param2" and compile it into a custom regular
> expression
> > and then match on that.
> >
> >
> >
> >
> >
> >
> >
> > On Thu, Apr 19, 2012 at 10:16 AM, Masiar <[email protected]> wrote:
> > > Imagine a service that sells goods. With the extensions I would like
> > > to have a programmer able to call init() passing as parameter a path,
> > > for example '/buy/:itemId',(I'm working on a protocol I won't explain
> > > in full details), and then my extension extends that path: thus it
> > > creates paths such as '/buy/:itemId/myStuff' and listen on that path
> > > for POST, GET, DELETE, PUT. That's it. Hope that's clearer.
> >
> > > P.S. I always worked with express.js, I don't know if there is
> > > something similar to the signature ":itemId" for example, to gather
> > > all the paths to that listener with some itemId. I used that signature
> > > to let you understand what I mean.
> >
> > > On Apr 19, 2:25 pm, Tim Caswell <[email protected]> wrote:
> > > > Could you be more specific?  What kind of paths are these.  Are you
> > > talking
> > > > a virtual filesystem, or http request urls or something else?
> >
> > > > On Thu, Apr 19, 2012 at 3:06 AM, Masiar <[email protected]> wrote:
> > > > > Hello everybody. Sorry for the meaningless title, I would like to
> > > > > create a Node.js extension that extends the paths the programmer
> > > > > writes adding some more to integrate a protocol I'm researching
> right
> > > > > now.
> > > > > The idea is the following: the programmer writes his/her server
> with
> > > > > his paths and stuff. When initialized, my extensions should create
> > > > > some a couple of listeners on some paths, for example:
> >
> > > > > var myExtension = require('./myExtension');
> > > > > //...
> > > > > myExtension.init(...);
> >
> > > > > and myExtension will create for example a listener to
> "/myextension/a"
> > > > > which will do something (decided by my extension).
> > > > > I wanted to know if this is somehow possible to do and if it is,
> how
> > > > > would you do that.
> > > > > Thanks,
> >
> > > > > Masiar
> >
> > > > > --
> > > > > Job Board:http://jobs.nodejs.org/
> > > > > Posting guidelines:
> > > > >https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > > > > You received this message because you are subscribed to the Google
> > > > > Groups "nodejs" group.
> > > > > To post to this group, send email to [email protected]
> > > > > To unsubscribe from this group, send email to
> > > > > [email protected]
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/nodejs?hl=en?hl=en
> >
> > > --
> > > Job Board:http://jobs.nodejs.org/
> > > Posting guidelines:
> > >https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > > You received this message because you are subscribed to the Google
> > > Groups "nodejs" group.
> > > To post to this group, send email to [email protected]
> > > To unsubscribe from this group, send email to
> > > [email protected]
> > > For more options, visit this group at
> > >http://groups.google.com/group/nodejs?hl=en?hl=en
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to