Overriding default express route URL handler - javascript

The default express router uses : (colon) to mark a parameter e.g.
users/:id
But when I was using other (non node.js) framework, I have noticed that they use curly brackets, e.g.
users/{id}
And I prefer the second style because browser escapes curly brackets unlike colon (Which I want to use in my URL), and, I just like it.
So, the question is, can I override default express route URL handler?

Unfortunately you cant. The Express.js framework relies on path-to-regexp library for extracting params from urls. And its usage is hardcoded inside the codebase, so you don't have any extension points to override that behavior.

Related

Create express route to match wildcard page but no pages under it

I'm using wildcard-subdomains to handle wildcard subdomains in my express app, so something like subdomain.localhost:3000 would enter the router as /s/subdomain/.
I want to match the main index url of any subdomain, but none of the pages under it.
Essentially, I want a route that would match /s/subdomain/ but not /s/subdomain/page/ or /s/subdomain/page (for clarity, "subdomain" is just a placeholder for any wildcard domain)
This is simple to do in regular regex, but I am unable to use the dot modifier due to express using path-to-regexp. It would be possible for me to generate a router for each user, but I would really like to use something cleaner if it's possible.
Dang, i'm pretty stupid. /s/:target/ works perfectly fine, and creates a neat little target parameter to make things a bit easier. I was totally overcomplicating things by trying to use regex.

Unexpected behavior for Express route regex generation

I'm writing a route in Express that should match URLs of the form /DDMMMYYYY, such as /01JUN2017 or /31JUL2014. My route is
app.get('/:date(\d{2}(JUN|JUL)\d{4})', ...);
but no expected URLS are matching.
According to Express Route Tester, this route is getting compiled to
/^\/(\d{2}\(JUN|JUL)\d\{4\}\)(?:\/(?=$))?$/i
which tells me that it is forcing a literal interpretation of the parenthesis before JUN, and the curly braces around 4. How can I tell Express to use these as special characters instead of literals?
You can't define groups within groups. There is an open issue about it.
Anyway, especifically for your case, you can do a workaround like:
app.get('/:date(\d{2}JU[NL]\d{4})', ...);

Is it possible to use any function for declare the path in express js get routing?

Actually, I want to create a web app in node js but when I used to special characters in my navigation path which I wanna go it shows "Cannot get /contest' ". So I want to try a function for sanitizing the path, remove the special characters from my URL and return actual path. How can it be possible in express js to get routing?
Express for Node supports regular expressions in routes.
There is also Router Tester available for you here.

Sending GET variables without "?"

I'm working on a website that is trying to call a link like this:
http://mysite/folder/g=foundation
I'm trying to troubleshoot some Javascript issues.
In the 'folder' subfolder there is an index.php that is looking for a 'g' GET variable.
As far as I'm aware this is incorrect and should be:
http://mysite/folder/?g=foundation
Where that would be sent to index.php in the 'folder' subfolder.
Is the first syntax ok? Is the '?' necessary for maybe having only one GET variable?
NOTE:
This whole site is completely working on a production server. This call works.
The version that breaks is on a newer Virtual Machine. Are there any configurations I can make to allow this kind of syntax?
The (?) question mark serves a purpose of indicating the initiation and declaration of query parameters. Excluding it would imply that you have a directory with an equals sign within the name.
RFC 1738 Uniform Resource Locators (URL) December 1994
3.3. HTTP
The HTTP URL scheme is used to designate Internet resources
accessible using HTTP (HyperText Transfer Protocol).
The HTTP protocol is specified elsewhere. This specification only
describes the syntax of HTTP URLs.
An HTTP URL takes the form:
http://<host>:<port>/<path>?<searchpart>
If, however, you must have it your way (without the question mark), you will need to use mod_rewrite.
To answer you question more literally. Yes. The (?) is necessary.
I believe this can be achieved with Apache rewrite module (mod_rewrite)
You can find a few examples here
http://www.sitepoint.com/apache-mod_rewrite-examples/
http://www.askapache.com/htaccess/mod_rewrite-basic-examples.html
Depends on the implementation of the server, but syntactically the ? should be required. Otherwise it looks like the browser is asking for an object named "g=foundation" with no parameters (rather than the default object with a parameter named g, which I believe is what you want).

AngularJS route without slash

in AngularJS can I set up a style route like this:
http://www.mydomain.com/title-and-more-irrelevant-text-123456-category.html
where '123546' is an indentifier ?
with:
$locationProvider.html5Mode(true);
I must make this because I need keep the old URLs
If this is the entire path, then I guess it will be hard because it is too generic.
If however you can live with paths in the form /prefix/title-and-stuff-123456-foo.html (i.e. the entire URL would look like http://mysrv.com/ctx/#/prefix/title-and-stuff-123.html), read on.
You can define the route as: /prefix/*titleAndId. Notice the *. Then the entire string is accessible inside any controller as $route.current.params.titleAndId. You can parse this string (using an appropriate regular expression perhaps) and extract any information, then load the appropriate content.
The * in the route definition enables the argument to contain slashes too. If slashes are not required, you can use /prefix/:titleAndId and use it the same way.

Categories