Skip to content

@victusfate /app.coffee forked from mizchi/app.coffee
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
express/coffeescript
# 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}."
<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>
<% } %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
<%- body %>
</body>
</html>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.