Skip to content

Instantly share code, notes, and snippets.

@heliostatic
Forked from bergie/README.md
Created May 19, 2011 16:03
Show Gist options
  • Save heliostatic/981123 to your computer and use it in GitHub Desktop.
Save heliostatic/981123 to your computer and use it in GitHub Desktop.
Falsy Values tutorials
http = require "http"
async = require "async"
fs = require "fs"
options =
host: "127.0.0.1"
port: 8003
path: "/"
method: "POST"
postData = (data, callback) ->
client = http.request options, (results) ->
results.setEncoding "utf-8"
content = ""
results.on "data", (ret) ->
content += ret
results.on "end", ->
callback null, content
client.write data
client.end()
server = http.createServer (req, res) ->
req.setEncoding "utf-8"
req.on "data", (content) ->
console.log "DATA: #{content}"
res.writeHead 200,
'Content-Type': 'text/plain'
res.end "Hey there"
data = ""
async.auto
start_server: (callback) ->
server.listen options.port, options.host, ->
callback null
read_first: (callback) ->
fs.readFile "first.txt", "utf-8", (err, content) ->
data += content
callback err, content
read_second: (callback) ->
fs.readFile "second.txt", "utf-8",(err, content) ->
data += content
callback err, content
post_to_server: ["start_server", "read_first", "read_second", (callback) ->
postData data, (err, retval) ->
server.close()
callback
]
async = require "async"
http = require "http"
fs = require "fs"
fetchPage = (path, host, callback) ->
options =
host: host or "www.midgard-project.org"
port: 80
path: path
method: "GET"
client = http.request options, (results) ->
content = ""
results.on "data", (chunk) ->
content += chunk
results.on "end", ->
callback "", content
client.end()
async.parallel [
(callback) ->
fetchPage "/", null, callback
,
(callback) ->
fetchPage "/documentation/midcom/", null, callback
,
(callback) ->
fetchPage "/download/", null, callback
,
(callback) ->
fetchPage "/updates/", null, callback
,
(callback) ->
fetchPage "/midcom-login-", null, callback
,
], (err, results) ->
file = fs.createWriteStream "foo.txt"
results.forEach (result) ->
file.write result
file.end()
# Create a web server
# Create an HTTP client in the same file
# POST data to your server
# Output the POST data
http = require "http"
options =
host: "127.0.0.1"
port: 8003
path: "/"
method: "POST"
server = http.createServer (req, res) ->
console.log "Got request"
req.setEncoding "utf-8"
req.on "data", (content) ->
console.log "DATA: #{content}"
res.writeHead 200,
'Content-Type': 'text/plain'
res.end "Hey there"
server.listen options.port, options.host, ->
client = http.request options, (results) ->
results.setEncoding "utf-8"
results.on "data", (content) ->
console.log content
client.write "Foo bar baz"
client.end()
# Create an expressjs server
express = require "express"
app = express.createServer()
app.use express.logger
format: ":method :url"
app.use express.cookieParser()
# Serve two different pages based on GET parameter "page"
# Set a cookie on the client
app.get "/", (req, res) ->
if req.query.page is "1"
res.cookie "foo", 1
return res.send "First page, cookie is #{req.cookies.foo}"
if req.query.page is "2"
res.cookie "foo", "2"
return res.send "Second page"
res.clearCookie "foo"
res.send "Index"
# Create a redirect from /old to /new
app.get "/old", (req, res) ->
res.redirect "/new"
app.get "/new", (req, res) ->
res.send "This is the new page"
app.listen 8003
# Create CommonJS module "fish"
# Provide functions to swim, mouthbreath, flap around
exports.swim = ->
"swims"
exports.mouthBreath = ->
"doesn't know how to mouthbreath"
exports.flapAround = ->
"happily flaps around"
# Create the basic Node.js http server
# Modify it to return some other text
# Make it return HTML
# Return the user-agent
# Return different text for at least two different browsers
http = require 'http'
browserSpecific = (agent) ->
if agent.indexOf("Chromium") isnt -1
return "an Agent of Google"
"something I don't recognize"
server = http.createServer (req, res) ->
res.writeHead 200,
'Content-Type': 'text/html'
'X-Powered-By': 'Coffee'
res.end "<h1>I'm learning Node</h1>
<p>And you're #{browserSpecific(req.headers['user-agent'])}"
server.listen 8003, '127.0.0.1'
http = require "http"
# Fetch the nytimes.com and output to console
options =
host: "www.nytimes.com"
port: 80
path: "/"
method: "GET"
request = http.request options, (results) ->
results.setEncoding "utf-8"
results.on "data", (content) ->
console.log content
request.end()
http = require "http"
fetchPage = (path, response, callback) ->
options =
host: "falsyvalues.com"
port: 80
path: path
method: "GET"
client = http.request options, (res) ->
response.statusCode = res.statusCode
for header, value of res.headers
response.setHeader header, value
res.on "data", (chunk) ->
response.write chunk
res.on "end", ->
callback "", response
client.end()
server = http.createServer (req, res) ->
console.log "Got request #{req.url}"
fetchPage req.url, res, (err, response) ->
response.end
server.listen 8003, "127.0.0.1"
# Import "Fish" module into another file and call the methods
fish = require("./fish")
console.log require.paths
console.log "Fish #{fish.swim()}"
console.log "Fish #{fish.mouthBreath()}"
console.log "Fish #{fish.flapAround()}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment