Skip to main content

Posts

Create a File Upload Service using Node.js / Express / Multer

Problem:    I needed to create a form that essentially uploads a file to a server, then posts the rest of the form data to a second server with the new file name. In other words, post the image to an “Image” server, then posts the form data (with the file URL) to a “Form Data” server. Solution:  Create an Express server that uses the “Multer” library to accomplish this. The Code: Create the express server and have it listen on a specific port var express = require( 'express' ); var app = express (); app . listen ( 3313 , function (){     console . log ( 'listening on port 3313' ); }); Install Multer.  Read here: https://www.npmjs.com/package/multer Include Multer to your express app.  We’ll be tweaking the storage properties so you can name the file whatever you like. At the end, you should have an “upload” object you’ll be using in the next step. be sure to change the file destination of your choice. var expre...

Dynamically Change an Element Width/Height Using CSS3 ONLY!

Problem: I needed to dynamically change the height/width of a div, but I didn't want to write another few lines of  javaScript. Is there a way I can find a pure css solution? you bet! Solution: Thanks to CSS3, we can now do this easily using the calc() function!         #myDiv {             width: calc(100% - 100px);             height: calc(100% - 100px);             margin: auto;             border: solid 1px #000000;         } More good news:  It works on all browsers :) Onward! References: http://www.w3schools.com/CSSref/func_calc.asp http://stackoverflow.com/questions/1192783/css-how-to-set-div-height-100-minus-npx

Node.js Looping and Async Calls: A Love Story

Problem :  I needed to create a node application that would ping a list of servers and return their status (if are they up or down).  All seemed easy enough until I needed to create asynchronous calls within a “for loop”.  The async method’s callback contained logic to simply build and return another object with the result data. Unfortunately, I couldn’t get ANY data back. Research :  After poking around some blogs and chatting with a coworker, I learned about node’s infamous “queue” of processes which fires AFTER my code finishes execution.  That is, when my “for loop” makes the async calls (let’s say, 5 times), the callback doesn’t fire until the end of my code is reached. Here’s what I initially coded: “returnObj” was serialized and written to the DOM well before the async callback was executed, writing a big, fat NOTHING to the browser.  This confused me for hours. What I needed to do was somehow write the response to the browser AFTE...

[Resolved] Sitecore ParseException: End of string expected at position...

Problem:  I have a line of code that uses Sitecore Fast Query to pull all items + children starting with a site item, like so: Item [] allItems = db.SelectItems( "fast:" + sitecorePath + "//*" ); Unfortunately, I would get a Sitecore parsing error at runtime: ParseException: End of string expected at position... Turns out Sitecore doesn't like hyphens ('-') in any sitecore path when using fast query, which I have a few distributor sites in a folder which contained hyphens. Solution: I create a simple method that resolves a sitecore path to be Sitecore fast query friendly:             string sitecorePath = "" ;             if (siteItem.Paths.FullPath.Contains( "-" ))             {                 String [...

SQL Server - Trim All Trailing "/" in a Table Column

Problem : Some of our redirects in our main web application were acting a little screwey. Turns out our stand-alone URL redirect app didn't play well with URLs with a trailing "/".  I needed to simply trim all trailing "/" from URLs that have them. Solution : Here's the script I created: BEGIN TRANSACTION        SELECT [url] AS ' url  with trailing "/"'        FROM    [URLRedirects] . [dbo] . [Redirects]        WHERE   RIGHT( [ url ] , 1 ) = '/'        UPDATE [Redirects]        SET     [ url ] = CASE RIGHT( [ url ] , 1 )                                          WHEN ...

Download a file directly to your "Downloads" folder without a dialog box

Problem: I built a custom web app and I wanted to download a log file directly to my downloads folder without a dialog . This app is only used by me and I specifically wanted all downloaded files in this location, no questions asked (by me, i guess) Research: I found many solutions that included a dialog box asking me where I want the file to be downloaded, but I wanted to bypass this process all together .  The key is this line of code: string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); I figured that getting the user would be hard to do, but this pretty much takes care of it. Solution: I added this to my dev tools.           protected bool SaveFileToDownloadsFolder(string fullFileName, string renamedFile)         {             string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);             stri...

Android http request crash fix executing getInputStream()

The Problem: In my Android app, I needed to make a simple call to a REST endpoint. I created a separate class extending the AysyncTask class like I am supposed to and as far as I was concerned, my code was clean and compiled just fine. Unfortunately, a runtime error occurred when getInputStream() was executed.  It jumped right to my “finally” block and the process ended suddenly. My Research: I added printStackTrace() to my finally block. When I caught and examined the stack trace, this is what I found: SecurityException: Permission denied (missing INTERNET permission?) The Solution: After extensive googling, I realized I needed to add a setting to my AndroidManifest.xml file:      <uses-permission android:name="android.permission.INTERNET" /> After running debugger, I finally made a successful htttp request. Here’s my code.      String json = "";          ...