d5 / node.native

C++11 port for the Node: native performance and modern simplicity.
MIT License
1.16k stars 149 forks source link

URL routing? #32

Open AlecTaylor opened 10 years ago

AlecTaylor commented 10 years ago

How do I work with URL routing in node.native?

E.g.: I want to expose /foo/ and /bar/ to echo different things.

jeremieca commented 10 years ago

You must implement your own routing system. In your server handler you have access to the informations about the request (request& req). With a simple "if" condition on the request url you'll be able to echo different things.

AlecTaylor commented 10 years ago

Hmm, is there a trick to printing the request URL?

I've tried:

std::cout << "req = " << req;

And:

std::cout << "req = " << std::string(req);

EDIT: Oh, native::http::request::~request() is private.

EDIT2: I can see the route by returning: res.end(req.url().path());, however neither req.url().path() == "/foo" nor strcmp(req.url().path(), "foo") are working. How do I construct the condition? - Or do I need to construct my own url_obj?

tclamb commented 10 years ago

That's really strange. req.url().path() == "/foo" should work.

Try playing with this gist to figure out how to route the way you want: https://gist.github.com/tclamb/8172136