Might be not the best example, but GET /thumbnail?size=large instead of GET /thumbnail?isLarge=true Or public gender: Gender instead of public isMale: boolean
February 21, 2018
Might be not the best example, but GET /thumbnail?size=large instead of GET /thumbnail?isLarge=true Or public gender: Gender instead of public isMale: boolean
January 23, 2013
Recursive remove directory for Node.js
It turned out that Node.js fs module does not have a method to remove the directory and its content recursively. Instead you should go through the directory structure and remove atomic items, i.e. individual files and empty directories. So I found a nice gist by Takuo Kihira at https://gist.github.com/2367067 made in JavaScript and decided to make a CoffeeScript version of it:
But in real life you should better use the system command:
child_process.exec "rm -rf #{directory}"
This might be much faster at the cost of platform dependency.
October 22, 2012
Deploy multiple Node applications on one web server in subdomains with Nginx
Earlier I wrote about deploying multiple Node applications on one web server in subfolders with Nginx.
Even though this approach is fully viable, you should not use it unless there are some really important reasons forcing you to go for it.
Given that the application is mounted to a subfolder, you should use relative URLs only in pages. Otherwise the application location must be configured in both Nginx and application itself. Use of relative URLs has a couple of major drawbacks:
- If you want to move a page within hierarchy, you need to update its content. For example, you have a page that makes an AJAX call to ../getValue. Once you move your page one level up or down in the hierarchy, it does not work.
- You need to solve a "trailing slash problem". For example, the page http://myhost/demo/pet-project/user-list must be also resolvable as http://myhost/demo/pet-project/user-list/. But if it refers a CSS file as ../css/user-list.css, the server will look for it at /opt/demo/pet-project/public/css/user-list.css and /opt/demo/pet-project/public/user-list/css/user-list.css correspondingly. It can be fixed by introducing the concept of canonical URLs and redirecting the client to the "proper" URL with or without trailing slash, depending on the chosen convention.
To minimize the maintenance complexity and avoid performance downgrade I decided to deploy Node applications in subdomains. With this approach I can freely use absolute URLs and thus easily shuffle my pages and have valid references without annoying redirects.
Nginx configuration for this setup is very similar to the one with subfolders and even a little bit more simple:
Finally you need to configure a DNS record for pet-project.myhost pointing to your server.
September 18, 2012
JSON format command line utility
Very often I need to format a single-line JSON, so I can read it easily.
Even though modern browser developer tools do it nicely with server responses in preview tab, I want to be able to do something similar with local files.
Node.js has a very good API to work both with JSON and file system. So I created this simple command line utility in CoffeeScript. It accepts the input file name as a 1st parameter and the output file as an optional 2nd parameter. If the output file name is undefined, it writes the result to the console.
July 12, 2012
Deploy multiple Node applications on one web server in subfolders with Nginx
There is no common approach for Node deployment.
Google can find tons of articles on this topic, but I was struggling to find the proper solution for the setup I need.
Basically, I have a web server and I want Node applications to be mounted to subfolders (i.e. http://myhost/demo/pet-project/) without introducing any configuration dependency to the application code.
At the same time I want other stuff like blog to run on the same web server.
Sounds simple huh? Apparently not.
In many examples on the web Node applications either run on port 80 or proxied by Nginx to the root.
Even though both approaches are valid for certain use cases, they do not meet my simple yet a little bit exotic criteria.
That is why I created my own Nginx configuration and here is an extract:
From this example you can notice that I mount my Pet Project Node application running on port 3000 to http://myhost/demo/pet-project.
First Nginx checks if whether the requested resource is a static file available at /opt/demo/pet-project/public/ and if so it serves it as is that is highly efficient, so we do not need to have a redundant layer like Connect static middleware.
Then all other requests are overwritten and proxied to Pet Project Node application, so the Node application does not need to know where it is actually mounted and thus can be moved anywhere purely by configuration.
proxy_redirect is a must to handle Location header properly. This is extremely important if you use res.redirect() in your Node application.
You can easily replicate this setup for multiple Node applications running on different ports and add more location handlers for other purposes.
UPDATE: Why and how you should do it in subdomains instead.
June 2, 2012
CoffeeScript version of the time span formatter between now and the date specified in a "Facebook way"
Earlier I published an ActionScript utility method that allows you to format the time span between now and the date specified in a "Facebook way".
Now I port it to CoffeeScript.
You can notice that the code is very similar, but thanks to CoffeeScript native string interpolation it is shorter and easier to read and understand!
You are welcome to contribute at https://github.com/skovalyov/time-format!June 1, 2012
Game Lines made with CoffeeScript and Stylus
The source code is available at https://github.com/skovalyov/game-lines.
A lot of improvements to be done, but you can already play!
I plan to optimize for mobile, make the animation smoother, handle game over properly, introduce different complexity levels and high scores: global, today and probably personal.
Stay tuned and you are welcome to join!
September 22, 2010
How to format the time span between now and the date specified in a "Facebook way"
Recently I was looking for some method to format the time span between now and the date specified in a "Facebook way". There are some, but none was completely what I was looking for. So I have decided to create mine. Moreover, it's not that complicated:
November 6, 2008
Do not comment code!
Commenting code is the worst practice promoted by "wannabe smart" books about software engineering. Joshua Block in his How to Design a Good API and Why it Matters keynote notes that code should read like prose and shows the good example:
And I completely agree with this statement. Thus good code is self-explanatory and does not require comments. On the other hand comments will never rescue bad code. Sure, we can use something like "TODO: I will fix this dummy trick in the next iteration", but no more than it.
May 12, 2008
Second Ukrainian Flash Platform User Group official meeting
April 17, 2008
Flex SDK coding conventions and best practices
March 17, 2008
First Ukrainian Flash Platform User Group official meeting
March 11, 2008
February 17, 2007
Text control with truncateToFit property support
As you probably know, Label control supports truncateToFit property, while Text doesn't. Even if you explicitly set it to true in your particular Text instance, nothing happens. I have created TruncatedText class that acts the similar way as Label does. If a TruncatedText instance is sized to be smaller than its text, the text is truncated with "...":
January 10, 2007
How to prevent pop-up blocking in Firefox
Using navigateToURL() method causes pop-up blocking feature activation in Firefox since Flash is a plug-in:
Use window.open() JavaScript method instead. I have created URLUtil class with openWindow() static method to make it easier:
Now my blog could be opened in a new browser window this way:
January 9, 2007
Left aligned label in FormItem
FormItem class doesn't have any styles or properties to control corresponding label alignment. Right alignment is applied by default and it's good idea to keep it "as is", because some usability researches say it's more friendly for user. Though if you want to make your FormItem label left aligned, use ExtendedFormItem class instead of regular FormItem:
ExtendedFormItem label is left aligned by default, but you can set labelAlign style to "right" if necessary.
December 28, 2006
Singleton in ActionScript 3.0
There are some ways to create singleton in ActionScript 3.0, though I prefer public "facade" with static initialization of internal class:
Internal class (it should be in the same package with "facade"):
Usage example:
December 27, 2006
Make the focused child visible!
Probably, one of the most annoying thing about Flex is lack of "browser native" features support. For example, if you have scrollable container it doesn't scroll its content upon Tab button click to make the focused child visible. However, browser do this! I have created easy to use class AutoScrollManager that makes it possible in Flex:
December 12, 2006
truncateToFit() performance problem
Setting very long string to the text property of classes that decorate UITextField (e. g. Label) causes timeout exception. Take a look at the truncateToFit() method in UITextField class. You could find this block there:
It means that actual text is cut char by char until result of its concatenation with truncation indicator fits into the width available or only one char left. When text is very long this looping is almost infinitive. I believe it would work much more faster with division by 2 approach. In this case even 64K text truncation will take only 16 loop cycles.