Created
July 21, 2012 03:39
-
-
Save adamvr/3154495 to your computer and use it in GitHub Desktop.
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
| node_modules |
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
| express = require 'express' | |
| request = require 'superagent' | |
| defaultPort = 3000 | |
| comic_path = (id) -> "http://www.smbc-comics.com/?db=comics&id=#{id}" | |
| port = process.env['PORT'] or defaultPort | |
| host = if port isnt defaultPort then '0.0.0.0' else '127.0.0.1' | |
| app = express.createServer() | |
| app.set 'views', '.' | |
| app.set 'view options', layout: false | |
| log = (msg) -> | |
| console.log "[#{(new Date).toISOString()}] #{msg}" | |
| app.get '/', (req, res) -> | |
| res.render 'index.jade' | |
| app.get '/comic', (req, res) -> | |
| id = req.query?.comic_id or 1 | |
| res.redirect "/comic/#{id}" | |
| app.get '/comic/:id', (req, res) -> | |
| id = req.params.id | 0 | |
| log "Rendering /#{id}" | |
| log "Requesting #{comic_path(id)}" | |
| request | |
| .get(comic_path(id)) | |
| .end (get_resp) -> | |
| comic = /comics\/([0-9-]*)\.gif/.exec(get_resp.text) | |
| if comic? | |
| log "Showing comic ID #{comic[1]}" | |
| res.render 'comic.jade', | |
| comic_id: comic[1], | |
| id: id, | |
| next_id: id + 1, | |
| prev_id: id - 1 | |
| else | |
| res.redirect '/' | |
| log "Listening on port #{host}:#{port}" | |
| app.listen port, host |
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
| html | |
| head | |
| title SMBC | |
| body | |
| div | |
| img(src="http://www.smbc-comics.com/comics/"+comic_id+".gif") | |
| div | |
| a(href="/comic/" + prev_id) Prev | |
| a(href='/') Main | |
| a(href="/comic/" + next_id) Next |
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
| html | |
| head | |
| title SMBC | |
| body | |
| h1 SMBC | |
| form(action='/comic', method='get') | |
| div | |
| label(for='comic_id') Enter a comic ID: | |
| input(id='comic_id', type='text', value='', name='comic_id') | |
| div | |
| input(type='submit', value= 'Get comic') | |
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
| { | |
| "name":"smbc-test", | |
| "version":"0.0.1", | |
| "private":true, | |
| "scripts": { | |
| "start": "coffee app.coffee" | |
| }, | |
| "dependencies": { | |
| "coffee-script": ">=1.3.3", | |
| "express":">=2.0.0", | |
| "jade":">=0.26.3", | |
| "superagent":">=0.0.1" | |
| }, | |
| "engines": { | |
| "node": "~0.6.0" | |
| } | |
| } |
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
| web: coffee app.coffee |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment