can somebody tell me how to use wildcards in the router table of node-http-proxy?
for example for wildcard subdomains, something like *.domain.de
i know there are RegEx used but i cant get it to work.
i tried like
'([a-zA-Z0-9_]).domain.de': '127.0.0.1:8085',
and
'([^.]*).domain.de' : '127.0.0.1:8085'
but none seem to redirect.
I've not done this myself but I would think that the whole string needs to be a regular expression. So it would be something like:
'[a-zA-Z0-9_]\.domain\.de': '127.0.0.1:8085',
Note the escaping of the dots. In fact, this would be simpler (though perhaps not as secure) if that format is correct:
'.*\.domain\.de': '127.0.0.1:8085',
Or even:
'\w*\.domain\.de': '127.0.0.1:8085',
Sadly, and as usual with all things Node, you are expected to "know" this stuff - mainly by reading the source code :( This is one of the key issues that puts me off using Node in the real world.
Related
I am trying to define a route in express js that takes an unknown amount N of parameters. It should match the following routes, capturing all digit groups:
/scope
/scope/1/12
/scope/1/12/123
etc.
I wrote a regex for the matching of the n-amount of numbers, as follows:
/(?:\/?(\d+)\/?)/g
The global /g however doesn't seem to be allowed, see (The regex parser of express js on github). Am I doing something wrong here? I could solve this very ugly and dirty by doing something like:
^\/scope\/?(\d+)?\/?(\d+)?\/?(\d+)?
But this is not dynamic, feels dirty and if I add deeper levels of scoping I always will need to add more /?(\d+) regex parts, which is a model that does not fit my business logic. I am shure there must be a better way...
Okay, after a discussion with #vks, which was useful but unfortunately not answering the question, we came to the conclusion that this is not a regex problem. With the \g modifier a regex capturing all digit groups can quite easily be written, even in javascripts very limited regex engine.
The question now becomes more clearly formulated: since expressjs does not allow a full regex from begin to end, but rather encloses the regex you use in a route in it's own begin and end of a regex, not allowing /g modifiers, what is the expressjs idiomatic way to solve this problem?
^\/scope(?:\/\d+)*$
You can try this.See demo.
https://regex101.com/r/eZ0yP4/30
I was wondering about some best practices regarding extraction of selectors to constants. As a general rule, it is usually recommended to extract magic numbers and string literals to constants so they can be reused, but I am not sure if this is really a good approach when dealing with selectors in Capybara.
At the moment, I have a file called "selectors.rb" which contains the selectors that I use. Here is part of it:
SELECTORS = {
checkout: {
checkbox_agreement: 'input#agreement-1',
input_billing_city: 'input#billing\:city',
input_billing_company: 'input#billing\:company',
input_billing_country: 'input#billing\:country_id',
input_billing_firstname: 'input#billing\:firstname',
input_billing_lastname: 'input#billing\:lastname',
input_billing_postcode: 'input#billing\:postcode',
input_billing_region: 'input#billing\:region_id',
input_billing_street1: 'input#billing\:street1',
....
}
In theory, I put my selectors in this file, and then I could do something like this:
find(SELECTORS[:checkout][:input_billing_city]).click
There are several problems with this:
If I want to know the selector that is used, I have to look it up
If I change the name in selectors.rb, I could forget to change it somewhere else in the file which will result in find(nil).click
With the example above, I can't use this selector with fill_in(SELECTORS[:checkout][:input_billing_city]), because it requires an ID, name or label
There are probably a few more problems with that, so I am considering to get rid of the constants. Has anyone been in a similar spot? What is a good way to deal with this situation?
Someone mentioned the SitePrism gem to me: https://github.com/natritmeyer/site_prism
A Page Object Model DSL for Capybara
SitePrism gives you a simple, clean and semantic DSL for describing
your site using the Page Object Model pattern, for use with Capybara
in automated acceptance testing.
It is very helpful in that regard and I have adjusted my code accordingly.
Consider the following sample codes:
1.Sample
var IsAdminUser = (User.Privileges == AdminPrivileges)
? 'yes'
: 'no';
console.log(IsAdminUser);
2.Sample
var IsAdminUser = (User.Privileges == AdminPrivileges)?'yes': 'no';
console.log(IsAdminUser);
The 2nd sample I am very comfortable with & I code in that style, but it was told that its wrong way of doing without any supportive reasons.
Why is it recommended not to use a single line ternary operator in Node.js?
Can anyone put some light on the reason why it is so?
Advance Thanks for great help.
With all coding standards, they are generally for readability and maintainability. My guess is the author finds it more readable on separate lines. The compiler / interpreter for your language will handle it all the same. As long as you / your project have a set standard and stick to it, you'll be fine. I recommend that the standards be worked on or at least reviewed by everyone on the project before casting them in stone. I think that if you're breaking it up on separate lines like that, you may as well define an if/else conditional block and use that.
Be wary of coding standards rules that do not have a justification.
Personally, I do not like the ternary operator as it feels unnatural to me and I always have to read the line a few times to understand what it's doing. I find separate if/else blocks easier for me to read. Personal preference of course.
It is in fact wrong to put the ? on a new line; even though it doesn’t hurt in practice.
The reason is a JS feature called “Automatic Semicolon Insertion”. When a var statement ends with a newline (without a trailing comma, which would indicate that more declarations are to follow), your JS interpreter should automatically insert a semicolon.
This semicolon would have the effect that IsAdminUser is assigned a boolean value (namely the result of User.Privileges == AdminPrivileges). After that, a new (invalid) expression would start with the question mark of what you think is a ternary operator.
As mentioned, most JS interpreters are smart enough to recognize that you have a newline where you shouldn’t have one, and implicitely fix your ternary operator. And, when minifying your script, the newline is removed anyway.
So, no problem in practice, but you’re relying on an implicit fix of common JS engines. It’s better to write the ternary operator like this:
var foo = bar ? "yes" : "no";
Or, for larger expressions:
var foo = bar ?
"The operation was successful" : "The operation has failed.";
Or even:
var foo = bar ?
"Congratulations, the operation was a total success!" :
"Oh, no! The operation has horribly failed!";
I completely disagree with the person who made this recommendation. The ternary operator is a standard feature of all 'C' style languages (C,C++,Java,C#,Javascript etc.), and most developers who code in these languages are completely comfortable with the single line version.
The first version just looks weird to me. If I was maintaining code and saw this, I would correct it back to a single line.
If you want verbose, use if-else. If you want neat and compact use a ternary.
My guess is the person who made this recommendation simply wasn't very familiar with the operator, so found it confusing.
Because it's easier on the eye and easier to read. It's much easier to see what your first snippet is doing at a glance - I don't even have to read to the end of a line. I can simply look at one spot and immediately know what values IsAdminUser will have for what conditions. Much the same reason as why you wouldn't write an entire if/else block on one line.
Remember that these are style conventions and are not necessarily backed up by objective (or technical) reasoning.
The reason for having ? and : on separate lines is so that it's easier to figure out what changed if your source control has a line-by-line comparison.
If you've just changed the stuff between the ? and : and everything is on a single line, the entire line can be marked as changed (based on your comparison tool).
Within Node.js, I am using querystring.stringify() to encode an object into a query string for usage in a URL. Values that have spaces are encoded as %20.
I'm working with a particularly finicky web service that will only accept spaces encoded as +, as used to be commonly done prior to RFC3986.
Is there a way to set an option for querystring so that it encodes spaces as +?
Currently I am simply doing a .replace() to replace all instances of %20 with +, but this is a bit tedious if there is an option I can set ahead of time.
If anyone still facing this issue, "qs" npm package has feature to encode spaces as +
qs.stringify({ a: 'b c' }, { format : 'RFC1738' })
I can't think of any library doing that by default, and unfortunately, I'd say your implementation may be the more efficient way to do this, since any other option would probably either do what you're already doing, or will use slower non-compiled pure JavaScript code.
What about asking the web service provider to follow the RFC?
https://github.com/kvz/phpjs is a node.js package that provides all the php functions. The http_build_query implementation at the time of writing this only supports urlencode (the query string includes + instead of spaces), but hopefully soon will include the enc_type parameter / rawurlencode (%20's for spaces).
See http://php.net/http_build_query.
RFC1738 (+'s) will be the default enc_type either way, so you can use it immediately for your purposes.
Example
www.domainname.com/page-param1-param2.html
...it will work fine but
www.domainname.com/page-param-1-param2.html
... here param one have the data like param-1 this time url is not working.
How do I get this to work?
Looks like you should use a different syntax, perhaps - there's no way to tell whether it should interpret the "1" as part of the first parameter or not. Alternatively, you could escape the - in "param-1" by encoding the - as "%2D" (no quotes).
So for example, your link would be: http://www.domainname.com/page-param%2D1-param2.html