background preloader

Node.js

Facebook Twitter

Node Tutorial Part 9. Welcome to part 9 of Let’s Make a Web App, a tutorial series about building a web app with Node. This series will walk you through the major areas you’ll need to face when building your own applications. These tutorials are tagged with lmawa. Previous tutorials: Updating connect-mongodb If you remember back to the start of this series, I had to write a hack to map a mongo connection string to the format connect-mongodb expected. Install the version of the package that I’m using: npm install connect-mongodb@0.1.1 Now update app.js: Remember Me Functionality Making logins persist in web apps involves some server-side work. When people log in, an extra “remember me” cookie is created The cookie contains the username and two random numbers (a series token and a random token) These values are also stored in the database When someone visits the site who isn’t logged in, if the cookie is present it’s checked against the database.

Building Remember Me This is basic Mongoose stuff. Views Controller. Node Tutorial Part 8. Welcome to part 8 of Let’s Make a Web App, a tutorial series about building a web app with Node. This series will walk you through the major areas you’ll need to face when building your own applications. These tutorials are tagged with lmawa. Previous tutorials: Flash Messages Flash messages are server-side messages that are displayed once. The session is usually used to store flash messages until they’re displayed, at which point they’re deleted. Express has support for flash messages through Connect’s flash middleware: req.flash('info', '%s items have been saved The first parameter is a category for the message.

Helpers Express has two kinds of view helpers: static and dynamic. I like to make a file called helpers.js with all of my helpers in, then load it with require: app.helpers(require('. Now update the Jade templates to use the helper: #{nameAndVersion(appName, version)} I’ve added this to the header in Nodepad. Adding Flash Message to Nodepad We’ll need a helper to display flash messages. Node Tutorial Part 7. Welcome to part 7 of Let’s Make a Web App, a tutorial series about building a web app with Node. This series will walk you through the major areas you’ll need to face when building your own applications. These tutorials are tagged with lmawa. Previous tutorials: Package Versions I’ve updated the Nodepad README to include the versions of Node and Mongo that I’m using. It also includes the versions of the packages I’ve used. Also remember that you need to restart Node whenever you change code (but not Jade templates).

We’re using npm to install packages, and it sets up path names so specific package versions can be required. Npm install express@1.0.0 Then to use it, do this: var express = require('express@1.0.0'); You can verify this works by typing node and entering the previous line: Jade Tricks When I first demonstrated Jade I hardcoded all the attributes. Notice that an ID selector has been combined with a class name: div#left.outline-view. Error Pages Error Handling within Mongoose Code.

Node Tutorial Part 6. Welcome to part 6 of Let’s Make a Web App, a tutorial series about building a web app with Node. This series will walk you through the major areas you’ll need to face when building your own applications. These tutorials are tagged with lmawa. Previous tutorials: Before starting this tutorial remember to start up a mongo daemon if your computer doesn’t run one automatically. In the last part we looked at authentication and sessions. Interface Design When I design interfaces I usually plan out a rough idea before developing the app.

I like to sketch out interfaces with graph paper, a mechanical pencil, and a good eraser. A simple sketch of Nodepad suggested the following: Cheating One of the most important things when building interfaces is to cheat as much as possible. Today there are a lot of solutions available, from CSS frameworks to GUI-heavy projects like Cappuccino. For the theme, I’ve decided to use Aristo (demo). Including Aristo and jQuery UI Page Structure Selecting Documents Progress. Node Tutorial Part 4. Welcome to part 4 of Let’s Make a Web App, a tutorial series about building a web app with Node. This series will walk you through the major areas you’ll need to face when building your own applications. These tutorials are tagged with lmawa. Previous tutorials: In this part I’ll add so much stuff you should get up and make a cup of tea first. By the end of the tutorial (or right now if you check the code out from git), you’ll have something that looks like this: Not all of the code is in this tutorial text: I’ve shortened a few code examples and haven’t included any CSS.

Updating Expresso Expresso has been updated to 0.70. Rendering Templates The document list method (/documents) should render a list of documents that we can edit. . … and a corresponding template: ul - for (var d in documents) li= d.title Remember that our templates are written with Express’s default language, Jade. Jade Using Jade is strange at first, but it’s actually pretty easy to get the hang of. Partials New and Edit Forms. Node Tutorial Part 3. Welcome to part 3 of Let’s Make a Web App, a tutorial series about building a web app with Node. This series will walk you through the major areas you’ll need to face when building your own applications. These tutorials are tagged with lmawa. Previous tutorials: In this part we will build on last week’s skeleton app. I already added a simple Document model, so let’s flesh that out a bit. These tutorials expect you to check out the code from the git repository, so visit nodepad to get it.

Logging Let’s add some logging. App.configure(function() { app.use(express.logger()); // Last week's configure options go here}); It’s usually a good idea to configure logging slightly differently, depending on environment. We can model accessing documents over HTTP using a CRUD-based (Create, Read, Update and Delete) RESTful API: The HTTP verbs are important — notice that the index and create methods have the same URL, but respond differently depending on whether a HTTP GET or PUT is used. Formats Tests. Node Tutorial Part 2. Welcome to part 2 of Let’s Make a Web App, a tutorial series about building a web app with Node.

This series will walk you through the major areas you’ll need to face when building your own applications. These tutorials are tagged with lmawa. Part 1 introduced this series and discussed how to select appropriate libraries for your Node projects. This part covers installing the basic tools and libraries, and we’ll also create a skeleton app and take a look at the generated code. Requirements This project depends on the following: A working Node install MongoDB npm I’ll walk you through the installation of each of these. Installation: Node If you don’t have Node installed download it and decompress it. You should be able to build and install Node like this: . You may need to change the permissions of the installation directory before running make install, or use sudo or su. These steps will work for a unix-based system. MongoDB I want to use MongoDB for our database. Npm . Packages Skeleton Analysis. Let's Make a Web App: Nodepad.

Welcome to part 1 of Let’s Make a Web App, a new tutorial series about building a web app with Node. This series will walk you through building a web app with Node, covering all the major areas you’ll need to face when building your own applications. The app we’re going to build is a web notepad called Nodepad. Not particularly original, but well-defined and easy to understand. Selecting Frameworks and Tools Modern web applications depend on several components: Storage: Relational database, NoSQL Storage library: simple, ORM Web server Package manager Server-side framework Client-side framework Testing libraries Version control Ultimately, the choice is down to context. In this case my criteria for selection will be based on feedback we’ve had about what our readers are interested in, and my own areas of expertise.

Server-Side Building web apps with Node typically involves a framework of some kind. An example of a popular Rails-like framework is Geddy. Not all frameworks are MVC, though. Next.