Some exercises from the Falsy Values workshops
-
-
Save geoffgarside/1494779 to your computer and use it in GitHub Desktop.
Falsy Values tutorials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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