Unexpected behavior for Express route regex generation - javascript

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})', ...);

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.

Overriding default express route URL handler

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.

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.

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.

How do you write a Javascript Regex that matches if file extension is not within (exe|js|htaccess)

I'm trying to write a javascript regexp for a blacklist of file extensions. I'm using a jquery plugin that has an option for acceptable file types that takes a regex, but rather than maintain a whitelist we would like to maintain a blacklist. So I need the regex to only match if the string doesn't contain certain file extensions. Currently there is a regex we use to whitelist photo extensions on our photo upload:
/(\.|\/)(gif|jpe?g|png)$/i
For our document upload we would like to simply do a blacklist, but I haven't been able to make the ?! delimeter work. So for the sake of an example how would I reverse this regex to match as long as the file extension doesn't contain gif, jpg, jpeg, png?
I've tried several different ways of using the ?!, but nothing I've tried has worked properly. Heres some examples of what I have tried unsuccessfully:
/(\.|\/)(?!gif|jpe?g|png)$/i
/(\.|\/)(?!(gif|jpe?g|png))$/i
Essentially I need this regex to always return true unless the blacklisted file extensions are matched.
This works:
/\.(?!(?:exe|js|htaccess)$)|\/(?!(?:exe|js|htaccess)$)/i
I think it's because the "match only if not followed by" doesn't like parentheses before it, but I'm not sure.
This new regex now works with the parentheses and multiple extensions:
/^.*(\.|\/)(?!(exe|js|htaccess)$)(?![^\.\/]*(\.|\/))/i
However, to allow files with no extension, this must be used:
/(^.*(\.|\/)(?!(exe|js|htaccess)$)(?![^\.\/]*(\.|\/)))|(^[^\.]+$)/i
// ^^^^^^^^^
// This part matches a name without any dots
Looking at the complexity of that regex, I suggest that you implement something server-side or use another plugin.
Hack the jQuery plugin to accept a callback function instead of (or in addition to) a regular expression. Use the negation operator (!) and the positive regular expression to supply an appropriate callback. And write a mail to the maintainer of the plugin asking him to accept your patch.
I'm not a regex expert, but something like this appears to work: /\.(exe|js|htaccess)$/ig.test(filename) true results when the file is on your blacklist.
var shouldblockUpload = /\.(exe|js|htaccess)$/ig.test(filename);
//Inform user illegal upload
You can also explicitly only allow certain filetypes through the accept attribute on file inputs to help hint users to the right ones.

Categories