public
Created — forked from mizchi/app.coffee

express/coffeescript

  • Download Gist
app.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
# load libraries
require 'coffee-script'
mongoose = require 'mongoose'
express = require 'express'
step = require "step"
 
# about oauth
OAuth = require('oauth').OAuth
oauth = new OAuth(
'https://api.twitter.com/oauth/request_token',
'https://api.twitter.com/oauth/access_token',
'<consumer key>',
'<consumer key secret>',
'1.0',
'http://localhost/oauth_verify',
'HMAC-SHA1')
 
# about mongoose
mongoose.connect 'mongodb://localhost/test'
 
User = mongoose.model 'o-users', new mongoose.Schema
id : Number
name : String
twitter:
token: String
token_secret: String
 
Post = mongoose.model 'post', new mongoose.Schema
id : Number
name : String
content : String
created_at : Date
 
# about express
app = express.createServer()
app.configure ()->
# for session ! write before app.router
app.use express.cookieParser()
app.use express.session { secret: 'mytestsessionkey' }
 
app.set 'views', __dirname + '/templates'
app.use express.methodOverride()
app.use express.bodyParser()
app.use app.router
app.use express.static(__dirname + '/static')
app.set 'view options', { layout: true }
app.set 'view engine', 'ejs'
 
app.configure 'development', ()->
app.use express.errorHandler({ dumpExceptions: true, showStack: true })
 
# ====== API ========
 
app.get '/oauth_verify',(req, res)->
oauth_token = req.query.oauth_token
oauth_verifier = req.query.oauth_verifier
 
if oauth_token and oauth_verifier and req.session.oauth
step ()->
oauth.getOAuthAccessToken oauth_token, null, oauth_verifier,@
return
, (e, @access_token, @access_token_secret, @results) ->
req.session.regenerate @
return
, ()->
req.session.name = @results.screen_name
req.session.twitter_id = @results.user_id
User.findOne { id:@results.user_id } ,@
return
, (e,user)->
console.log user
if not user
item = new User()
item.name = @results.screen_name
item.id = @results.user_id
item.twitter =
token: @access_token
token_secret: @access_token_secret
item.save (e)-> console.log 'add new user:'+JSON.stringify(item)
console.log "[login] #{@results.screen_name}" if user
res.redirect '/'
return
else
console.log "[login] #{@results.screen_name}" if user
res.redirect '/'
return
else
oauth.getOAuthRequestToken (error, oauth_token, oauth_token_secret, results)->
req.session.oauth =
oauth_token: oauth_token
oauth_token_secret: oauth_token_secret
request_token_results: results
res.redirect 'https://api.twitter.com/oauth/authorize?oauth_token=' + oauth_token
# logout
app.get '/logout', (req, res)->
req.session.destroy ()->
res.redirect '/'
 
"""
add post
needed : login
param : content
"""
app.get '/add_post', (req, res)->
if not req.session.twitter_id or not req.query.content
res.send "not enough post data"
return
item = new Post()
item.name = req.session.name
item.id = parseInt(req.session.twitter_id)
item.content = req.query.content
item.created_at = new Date
 
item.save (err) ->
console.log 'add new post:'+JSON.stringify(item) unless err
res.redirect("/");
 
# ====== URL Map ========
 
# index
app.get '/', (req, res)->
login_name = ""
items = []
login_name = req.session.name if req.session.name
if login_name
Post.find { id:req.session.twitter_id }, (err,posts)->
res.render "index.ejs"
items: posts.reverse()
login_name: login_name
layout: true
else
User.find {},(err,users) ->
res.render "index.ejs"
items: []
login_name: login_name
layout: true
 
# Bind Application
port = 3000
app.listen port
console.log "run server. port #{port}."
index.ejs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<h1>Object List</h1>
 
 
<% if(login_name) { %>
<p> you logged in as <%= login_name %> : <a href="/logout"> logout </a></p>
 
 
<form method="GET" action="/add_post">
update:<input type="text" name="content" id="content" />
<input type="submit" />
</form>
<% } else {%>
<a href="/oauth_verify"> oauth </a>
<% } %>
 
 
<% if (items.length) { %>
<ul>
<% items.forEach(function(i){ %>
<li><%= i.name %>:<%= i.content %> </li>
<% }) %>
</ul>
 
<% } %>
layout.ejs
1 2 3 4 5 6 7 8 9 10
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
<%- body %>
</body>
</html>`

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.