├── .gitignore ├── LICENSE ├── README.md ├── bower.json ├── index.js ├── package.json └── public ├── app.js └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jacob Carter 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Twitter-API-with-Node-and-Express 2 | Utilize Twitter JS Client with a node/express setup 3 | 4 | ## Installation 5 | 6 | ``` 7 | npm install twitter-api-node-express 8 | ``` 9 | 10 | OR 11 | 12 | ``` 13 | bower install twitter-api-node-express 14 | 15 | cd ./bower_components/twitter-api-node-express/ 16 | 17 | npm install 18 | ``` 19 | 20 | ##Notes 21 | 22 | This uses [BoyCook's](https://github.com/BoyCook) TwitterJS Client. [See configuration of that here](https://github.com/BoyCook/TwitterJSClient/blob/master/README.md). 23 | 24 | ##Run Application 25 | 26 | From the twitter-api-node-express directory: 27 | 28 | `node index.js` 29 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "twitter-api-node-express", 3 | "homepage" : "https://github.com/jacobscarter/Twitter-API-with-Node-and-Express", 4 | "version" : "0.1.0" 5 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./node_modules/twitter-node-client/lib/Twitter'); 2 | 3 | var express = require('express'); 4 | var app = express(); 5 | var bodyParser = require('body-parser'); 6 | 7 | var error = function (err, response, body) { 8 | console.log('ERROR [%s]', JSON.stringify(err)); 9 | }; 10 | var success = function (data) { 11 | console.log('Data [%s]', data); 12 | }; 13 | 14 | var config = { 15 | "consumerKey": "b7zg7k8fcPvK27sHIOYOwMSqG", 16 | "consumerSecret": "orhgMLC0DVxkkELhAYaMw14685FWWmYzhrzxrKtAtT6nlaI9M8", 17 | "accessToken": "855355436-Q4bbLZflQuoHP3I5PtQjtXrMDaSQ0WXnM5DXa3TG", 18 | "accessTokenSecret": "cYFg5epLwcmDnBzLqImEt45Wg60qUDaZUc6nxDgXz6s3P" 19 | }; 20 | 21 | var twitter = new module.exports.Twitter(config); 22 | 23 | app.use( bodyParser.json() ); // to support JSON-encoded bodies 24 | app.use(bodyParser.urlencoded({ // to support URL-encoded bodies 25 | extended: true 26 | })); 27 | 28 | /* 29 | * To connect to a front end app (i.e. AngularJS) store all your files you will * 30 | * statically store in declared below (i.e. ./public) * 31 | */ 32 | 33 | app.use(express.static('public')); 34 | 35 | //post to retrieve user data 36 | app.post('/twitter/user', function (req, res) { 37 | var username = req.body.username; 38 | var data = twitter.getUser({ screen_name: username}, function(error, response, body){ 39 | res.status(404).send({ 40 | "error" : "User Not Found" 41 | }); 42 | }, function(data){ 43 | res.send({ 44 | result : { 45 | "userData" : data 46 | } 47 | }); 48 | }); 49 | }); 50 | 51 | 52 | var server = app.listen(3000, function () { 53 | var host = server.address().address; 54 | var port = server.address().port; 55 | }); 56 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"twitter-api-node-express", 3 | "description":"Twitter JS Client Hooked in with Node and Express", 4 | "version":"0.1.0", 5 | "author" : "Jacob Carter", 6 | "dependencies" : { 7 | "twitter-node-client" : "*", 8 | "body-parser": "^1.12.2", 9 | "express": "^4.12.3" 10 | }, 11 | "license": "MIT" 12 | } 13 | -------------------------------------------------------------------------------- /public/app.js: -------------------------------------------------------------------------------- 1 | var app = angular.module('myApp', []); 2 | 3 | app.controller('myCtrl', function($scope, TwitterService){ 4 | $scope.getUser = function(username){ 5 | console.log("username entered ", username); 6 | TwitterService.getUser(username) 7 | .then(function(data){ 8 | $scope.twitterErrors = undefined; 9 | $scope.results = JSON.parse(data.result.userData); 10 | }) 11 | .catch(function(error){ 12 | console.error('there was an error retrieving data: ', error); 13 | $scope.twitterErrors = error.error; 14 | }) 15 | } 16 | 17 | }); 18 | 19 | app.factory('TwitterService', function($http, $q){ 20 | 21 | var getUser = function(username){ 22 | var d = $q.defer(); 23 | $http.post('/twitter/user', {username : username}) 24 | .success(function(data){ 25 | return d.resolve(data); 26 | }) 27 | .error(function(error){ 28 | return d.reject(error); 29 | }); 30 | return d.promise; 31 | }; 32 | 33 | return { 34 | getUser : getUser 35 | } 36 | }); -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Twitter API with Javascript/Node/Express 5 | 6 | 7 | 8 | 9 | 10 |
11 |
12 |
13 |

Twitter API with Javascript (node/express)

14 |
15 |
16 |
17 |
18 | 19 |
20 |
21 |

22 |
23 |
24 |
25 |
26 |
27 | {{twitterErrors}} 28 |
29 |
30 |
31 |
32 |
33 |

34 | 35 | 36 | 39 | 42 | 43 |
37 | {{key}} 38 | 40 | {{value}} 41 |
44 |
45 |
46 |
47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | --------------------------------------------------------------------------------