Showing
5 changed files
with
43 additions
and
74 deletions
| 1 | + | ||
| 2 | + | ||
| 1 | module.exports = function(app) | 3 | module.exports = function(app) |
| 2 | { | 4 | { |
| 3 | app.get('/',function(req,res){ //index.html 가져오기 | 5 | app.get('/',function(req,res){ //index.html 가져오기 |
| 4 | res.render('index.html') | 6 | res.render('index.html') |
| 5 | }); | 7 | }); |
| 6 | 8 | ||
| 7 | - app.get('/timeline/:id',function(req,res){ | 9 | + // app.get('/timeline/:id',function(req,res){ |
| 8 | - res.render('timeline.html') | 10 | + // res.render('timeline.html') |
| 9 | - }); | 11 | + // }); |
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
| 10 | 15 | ||
| 11 | } | 16 | } |
| 17 | + | ... | ... |
| 1 | -var express = require('express'); | 1 | +var express = require('express'); //express 모듈 불러오기 |
| 2 | var app = express(); | 2 | var app = express(); |
| 3 | +const tweetsController = require('./twitter-controller'); | ||
| 3 | 4 | ||
| 4 | var router = require('./router/main')(app);// 라우터 모듈인 main.js 를 불러와서 app 에 전달해줍니다. | 5 | var router = require('./router/main')(app);// 라우터 모듈인 main.js 를 불러와서 app 에 전달해줍니다. |
| 5 | app.set('views', __dirname + '/views'); //서버가 읽을 수 있도록 HTML 의 위치를 정의해줍니다. | 6 | app.set('views', __dirname + '/views'); //서버가 읽을 수 있도록 HTML 의 위치를 정의해줍니다. |
| 6 | app.set('view engine', 'ejs'); //서버가 HTML 렌더링을 할 때, EJS 엔진을 사용하도록 설정합니다. | 7 | app.set('view engine', 'ejs'); //서버가 HTML 렌더링을 할 때, EJS 엔진을 사용하도록 설정합니다. |
| 7 | app.engine('html', require('ejs').renderFile); | 8 | app.engine('html', require('ejs').renderFile); |
| 8 | 9 | ||
| 10 | +app.get('/timeline/:screen_name',tweetsController.getUserTweets); // '/timeline/:screen_name'형식의 url이 들어오면 뒤의 함수를 실행시킴 | ||
| 11 | + | ||
| 9 | var server = app.listen(3000, function(){ //3000 포트 사용 | 12 | var server = app.listen(3000, function(){ //3000 포트 사용 |
| 10 | - console.log("Express server has started on port 3000") | 13 | + console.log("Express server has started on port 3000"); |
| 11 | }) | 14 | }) |
| 12 | 15 | ||
| 13 | app.use(express.static('public')); | 16 | app.use(express.static('public')); | ... | ... |
twitter-controller.js
0 → 100644
| 1 | +const Twitter = require('twitter'); | ||
| 2 | + | ||
| 3 | +const client = new Twitter({ | ||
| 4 | + consumer_key: 'Q4xyL4HBupStqkUf3FaKeDlSL', | ||
| 5 | + consumer_secret: 'xB9ROWLAPlPW7tntBUsFgVZd9qcaCSDDAo5fFCH1qWg7oAwJLO', | ||
| 6 | + access_token_key: '1330868660072660992-0l3jauBmdEP16hXPviH5W1DMS46X9B', | ||
| 7 | + access_token_secret: 'jx5xtDHam5SUTSndp7uVqsTpbSJiD4OIKL8IYKg1ZtTSZ' | ||
| 8 | + | ||
| 9 | +}); | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +exports.getUserTweets = async function(req, res){ | ||
| 13 | + try{ | ||
| 14 | + let data = client.get('statuses/user_timeline', req.params, function(error,tweets,response){ //트위터 api에서 유저의 타임라인을 가져옴 req.params에 유저 아이디가 들어있음 | ||
| 15 | + if(!error){ | ||
| 16 | + console.log(tweets); //가져온 타임라인 내용 콘솔창에 출력 | ||
| 17 | + res.render('timeline.html',tweets); //timeline.html 화면에 뿌려줌 그리고 tweets값을 저 페이지로 보냄 | ||
| 18 | + } | ||
| 19 | + }); //아이디를 토대로 타임라인 가져오기 | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + }catch(err){ //에러 발생하면 실행 | ||
| 23 | + console.log(err); | ||
| 24 | + res.sendStatus(500); | ||
| 25 | + } | ||
| 26 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -23,8 +23,8 @@ | ... | @@ -23,8 +23,8 @@ |
| 23 | <button class="btn-search" type="button" onclick="movePage()">SEARCH</button> | 23 | <button class="btn-search" type="button" onclick="movePage()">SEARCH</button> |
| 24 | </div> | 24 | </div> |
| 25 | <script type ="text/javascript"> | 25 | <script type ="text/javascript"> |
| 26 | - function movePage(){ //페이지 이동을 위한 함수 | 26 | + function movePage(){ //페이지 이동을 위한 함수 search버튼을 누르면 실행됨 |
| 27 | - location.href ="/timeline/"+document.getElementById('search').value | 27 | + location.href ="/timeline/"+document.getElementById('search').value //url을 이렇게 변경함 |
| 28 | } | 28 | } |
| 29 | </script> | 29 | </script> |
| 30 | </div> | 30 | </div> | ... | ... |
| ... | @@ -3,76 +3,10 @@ | ... | @@ -3,76 +3,10 @@ |
| 3 | <body> | 3 | <body> |
| 4 | 4 | ||
| 5 | <script> | 5 | <script> |
| 6 | - | ||
| 7 | alert("Hello, world!"); | 6 | alert("Hello, world!"); |
| 8 | 7 | ||
| 9 | -</script> | ||
| 10 | - | ||
| 11 | -<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> | ||
| 12 | -<script src="http://oauth.googlecode.com/svn/code/javascript/oauth.js"></script> | ||
| 13 | -<script src="http://oauth.googlecode.com/svn/code/javascript/sha1.js"></script> | ||
| 14 | -<script type="text/javascript"> | ||
| 15 | -$(function(){ | ||
| 16 | - fn_twitterTimeline(); | ||
| 17 | -}); | ||
| 18 | - | ||
| 19 | -function fn_twitterTimeline(){ | ||
| 20 | - var twitterPrm = { | ||
| 21 | - api: 'https://api.twitter.com/1.1/statuses/user_timeline.json', | ||
| 22 | - count:100, | ||
| 23 | - callback:"fn_makedocTwitter", | ||
| 24 | - | ||
| 25 | - consumerKey:"Q4xyL4HBupStqkUf3FaKeDlSL", | ||
| 26 | - consumerSecret:"xB9ROWLAPlPW7tntBUsFgVZd9qcaCSDDAo5fFCH1qWg7oAwJLO", | ||
| 27 | - accessToken:"1330868660072660992-0l3jauBmdEP16hXPviH5W1DMS46X9B", | ||
| 28 | - tokenSecret:"zQjx5xtDHam5SUTSndp7uVqsTpbSJiD4OIKL8IYKg1ZtTSZ" | ||
| 29 | - | ||
| 30 | - } | ||
| 31 | - var oauthMessage={ | ||
| 32 | - method:"GET", | ||
| 33 | - action:twitterPrm.api, | ||
| 34 | - parameters:{ | ||
| 35 | - count: twitterPrm.count, | ||
| 36 | - callback:twitterPrm.callback, | ||
| 37 | - oauth_version:"1.0", | ||
| 38 | - oauth_signature_method:"HMAC_SHA1", | ||
| 39 | - oauth_consumer_key:twitterPrm.consumerKey, | ||
| 40 | - oauth_token:twitterPrm.accessToken | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - }; | ||
| 44 | - OAuth.setTimestampandNonce(oauthMessage); | ||
| 45 | - OAuth.SignatureMethod.sign(oauthMessage,{ | ||
| 46 | - consumerSecret: twitterPrm.consumerSecret, | ||
| 47 | - tokenSecret:twitterPrm.tokenSecret | ||
| 48 | - }); | ||
| 49 | - | ||
| 50 | - var twJsonpath=OAuth.addToURL(oauthMessage.action,oauthMessage.parameters); | ||
| 51 | - | ||
| 52 | - $.ajax({ | ||
| 53 | - type:oauthMessage.method, | ||
| 54 | - url:twJsonpath, | ||
| 55 | - dataType:"jsonp", | ||
| 56 | - jsonp:false, | ||
| 57 | - cache:true | ||
| 58 | - }); | ||
| 59 | -} | ||
| 60 | - | ||
| 61 | -function fn_makedocTwitter(data){ | ||
| 62 | - <DIV id="search"></DIV> | ||
| 63 | - var timeLine=''; | ||
| 64 | - var text; | ||
| 65 | - var dateObj=new Data; | ||
| 66 | - for(var i=0,len=data.length;i<len;i++){ | ||
| 67 | - text=data[i].text; | ||
| 68 | - text=text.replace(/(s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:@&=+$,%#]+)/gi,'<a href="$1">$1</a>'); | ||
| 69 | - text=text.replace(/@(\w+)/gi,'<a href="http://twiiter.com/search?q=%23$1">#$1</a>'); | ||
| 70 | - text.text.replace(/@(\w+)/gi,'<a href="http://twitter.com/$1">@$1</a>'); | ||
| 71 | - timeLine+="<p>"+text+"</p><p>"+dateObj+"</p>"; | ||
| 72 | 8 | ||
| 73 | - } | 9 | +console.log(tweets); |
| 74 | - $('#div_snstwitter').html(timeLine); | ||
| 75 | -} | ||
| 76 | 10 | ||
| 77 | 11 | ||
| 78 | </script> | 12 | </script> | ... | ... |
-
Please register or login to post a comment