I had trouble posting to my Rails server from an Angular controller until I found this SO post. In
/app/controllers/application_controller.rb I added the following code.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery?
# In Rails 4.2 and above
super || valid_authenticity_token?(session, request.headers['X-XSRF-TOKEN'])
An alternate title might be: how to bind event handlers to new, or dynamically generated, table rows. When using jQuery, we attach sets of instructions to different parts of an HTML document using selectors. Suppose you wanted to attach some functionality to some text in a table, for example, when you click on a row in a table, it brings up a new table below the first table. This is actually kind of tricky, it turns out that there is a distinction between direct and delegated event handlers.
Continue reading Direct and Delegated Event Handlers in jQuery
I created a fairly straight-forward node application with socket.io, but when I put it into production, I couldn’t get it to work. It was dead on arrival. After looking up a ton of examples I figured out what the problem was. (I apologize in advance if this is obvious to seasoned node developers.) Assuming everything else is running…
Continue reading Getting socket.io to Work in the Wild
When a node application crashes unexpectedly, you might get a
EADDRINUSE, Address already in use error when you try to restart it. This means that the port you’re using is already being used by the crashed application. What you need to do is fun the PID of that process, and then kill it. There’s more than one way to skin that cat, but I like to use
netstat. N.B. You’ll need to run this as
sudo for it to work.
Continue reading Handle EADDRINUSE Error in Node
I had some trouble this weekend figuring out how to open a text file in Angular. My luck turned around when I found this StackOverflow question. The question addressed loading an image, but I was able to fiddle with this Plunker example until I was able to load text files. (Basically, I replaced every instance of “readAsDataURL” in the upload.js file with “readAsText”.)
Continue reading Open a Text File with Angular
In order to create your own modules in Node, you attach your own classes or functions to an
module.exports object. Then you can access those items in the required module. Suppose we have a file named
classes.js. If we wanted to import the
ClassY classes from that module, then we would add the following lines at the end of the
module.exports.ClassX = ClassX ;
module.exports.ClassY = ClassY ;
Then, in another file we would access these classes as:
var classes = require("./classes.js") ;
var ClassX = classes.ClassX ;
var ClassY = classes.ClassY ;
Continue reading Preserve JS Object Methods Over HTTP via JSON
I am taking a course on MongoDB development with Node.js from Mongo University. In the second week we covered a thing that I thought was very interesting. They walked you through how to grab the JSON data out of a Reddit page. Reddit apparently offers its data up as a JSON if you pass it a
.json path. Here is the coffeescript that produces the code provided in the development course.
Continue reading Using Node and Mongo to Collect Data from Reddit