Skip to content

Node.js Cheat Sheet

Lance Pollard edited this page Sep 25, 2012 · 2 revisions

Executables

Execute one-off commands

exec = require('child_process').exec

exec 'ls -la', (error, stdout, stderr) ->

Execute child processes

spawn = require('child_process').spawn

child = spawn 'node', ['server.js']
child.setEncoding('utf-8')
child.stdout.on 'data', (data) ->
child.stderr.on 'data', (data) ->

Create Executable Node.js Script

#!/usr/bin/env node

console.log('executing', __filename);

File System

Here are the modules that will be used below:

fs = require('fs')

Reading Files

fs.readFile './bin/tower', 'utf-8', (error, string) ->

string = fs.readFileSync './bin/tower', 'utf-8'

Writing Files

fs.writeFile './tmp/README.md', '# Writing Files', (error) ->

fs.writeFileSync './tmp/README.md', '# Writing Files'

Watching Files

HTTP Requests

Clone this wiki locally