background preloader

Develop a RESTful API Using Node.js With Express and Mongoose...

Develop a RESTful API Using Node.js With Express and Mongoose...

moshen/node-googlemaps Serving Static Files from Node.js Posted: 5/6/2011 12:14 PM In the last post I showed you how to get started with Node.js on Windows. Easy, wasn’t it? Remarkably so since there was no install requirement. Note, for posterity's sake, that all of this is based on node.js as of version 0.4.7. Making Things Easier Last time we got started by unzipping the file that contained all the node files. Computer > Right-click and hit properties > Advanced System Settings (on the left) > Environment Variables > System Variables > Path > add “C:\Program Files\Node” So now the files that we use can be in a directory all their own and you still only have to type “node.exe server.js” to get started. And another tip, keep Fiddler open while you mess around with node. Serving Up A File So starting a server and returning some Html hard-coded in Javascript is awesome like we did last time, but it would be even better if we could do this by serving up Html files. First, let’s create a very basic html page. Serving Up Files And voila!

Vows « Asynchronous BDD for Node NowJS and Node.js Tutorial – Creating a multi room chat client | Aditya Ravi Shankar Node.js is a server side environment for Javascript. NowJS is a framework built on top of Node.js that connects the client side and server side Javascript effortlessly. The core of NowJS functionality lies in the now object.The now object is special because it exists on the server and the client.This means variables you set in the now object are automatically synced between client and server. Also server functions can be directly called on the client and client functions can be called directly from the server.All in realtime. You can have a working HTTP server up and running in Node.JS with just a few lines of code. This little snippet of code will create an HTTP server, listen on port 8080, and send back “Hello World” for every request. Using NowJS, communication between the client and server side is just as simple. Client Side: <script type="text/javascript"> now.ready(function(){ now.clientSideVariable = 'someValue'; now.serverSideFunction(); }); </script> Server Side: 1. 2. 3. 4. 5.

Things I wish I knew about MongoDB a year ago I’ve used MongoDB for over a year at scale at both Heyzap and Bugsnag and I’ve found it to be a very capable database. As with all databases, there are some gotchas, and here is a summary of the things I wish someone had told me earlier. Selective counts are slow even if indexed For example, when paginating a users feed of activity, you might see something like, In MongoDB this count can take orders of magnitude longer than you would expect. Inconsistent reads in replica sets When you start using replica sets to distribute your reads across a cluster, you can get yourself in a whole world of trouble. This is compounded if you have performance issues that cause the replication lag between a primary and its secondaries to increase to minutes or even hours in some cases. You can control whether a query is run on secondaries and also how many secondaries are replicated to during the insert, but this will affect performance and could block forever in some cases! Mongo’s BSON ID is awesome Profiler

Developer Network From game jam to mobile and Firefox OS I love participating in game jams, where game developers get together to craft games in a very short amount of time. I thought it would be cool to take one of my past Game Jam games, Metal vs Hipsters, and port it to the mobile web and Firefox OS. Adapting ... Web Push notifications from Irssi Our main communication tool at Mozilla is IRC. I’m running an IRC client called Irssi under screen on a server constantly connected to the network.

Alexander Luksidadi's Blog » ExpressJS without Jade? Use Underscore template! Many of you must have felt like a burden knowing that Express recommended you to learn another template language (Jade). Don’t worry, you can code all your templates on HTML using underscoreJS! Oh yay? Let’s take a look on how you implement that on your express app. First install express package, create your express app: $ npm install -g express $ express . Install your underscore package $ npm install -d underscore If you edit Now, all you need to do is, to comment out 1 line and register underscorejs: Now, go to routesindex.js : $ vi routes/index.js Change the template name from ‘index’ to ‘index.html’: Next, go to views directory and create layout.html And last, still in views directory, create another file called index.html And there you go.. you can write your HTMl code in peace =)

Designing Better JavaScript APIs Advertisement At some point or another, you will find yourself writing JavaScript code that exceeds the couple of lines from a jQuery plugin. Your code will do a whole lot of things; it will (ideally) be used by many people who will approach your code differently. They have different needs, knowledge and expectations. This article covers the most important things that you will need to consider before and while writing your own utilities and libraries. We’ll focus on how to make your code accessible to other developers. Peter Drucker once said: “The computer is a moron.” Table of Contents Fluent Interface The Fluent Interface1 is often referred to as Method Chaining (although that’s only half the truth). Aside from major simplifications, jQuery offered to even out severe browser differences. Method Chaining The general idea of Method Chaining6 is to achieve code that is as fluently readable as possible and thus quicker to understand. Command Query Separation Going Fluent Consistency Callbacks

Botsikas' Blog: Node.js modules cross platform compilation using gyp Update: I have made a pull request where you can find the updated tools discussed in this article, located here Node.js has been using waf (node-waf) to configure and build modules up to version 0.4. From v0.6 and on, the team has moved on to gyp (Generate Your Projects) which seems to be a bit more promising when it comes to cross platform compilation. This post shows how to create a simply gyp file to build your own custom native node.js modules and provides some scripts to automate the project generation process. A bit of history Gyp is a google project that was created to support cross platform building of the opensource chromium project. Node-waf vs gyp Up to version 0.4 the node.js team offered node-waf (a waf 1.5.3 wrapper script) to configure and build modules for node.js. Node Module’s gyp file I have edited a simple gyp file (see the end of this post for source code) to compile the simple hello world native nodejs module I have used in my previous posts (here and here).

Getting Started with Node.js on Heroku node Table of Contents This quickstart will get you going with Node.js and the Express web framework, deployed to Heroku. For general information on how to develop and architect apps for use on Heroku, see Architecting Applications for Heroku. Prerequisites If you’re new to Heroku or Node.js development, you’ll need to set up a few things first: A Heroku user account. Local workstation setup Once installed, you can use the heroku command from your command shell. heroku login Enter your Heroku credentials. Press enter at the prompt to upload your existing ssh key or create a new one, used for pushing code later on. Write your app You may be starting from an existing app. var express = require("express"); var logfmt = require("logfmt"); var app = express(); app.use(logfmt.requestLogger()); app.get('/', function(req, res) { res.send('Hello World!') Declare dependencies with npm Heroku recognizes an app as Node.js by the existence of a package.json file. Declare process types with Procfile Console

style guide Opinions are like assholes, every one has got one. This one is mine. Punctuation: who cares? Punctuation is a bikeshed. Put your semicolons, whitespace, and commas where you like them. This post is concerned with higher-order style. Be obvious Don't do something complex just to make your api simpler. Example, avoid chaining DSLs. This is bad: thing.when('something').then(doThing) It's not really obvious how when relates to dothing. Chaining where you simply return this is acceptable. Be idiomatic, or not. if possible, make your code follow the APIs in node core. If you don't do this, you need documentation. if your can't follow an idiomatic API precisely, do something completely different. ALWAYS PASS ERR IN CALLBACK, (an event listener is not a callback, so this doesn't apply in that case) If you have a function called createServer, it should return a server, and it should have a listen function. createServer should never start the server listening. "all you need is lambdas" -- John Lennon

Related: