Search code examples
javascriptnode.jsexpressoauthgrant-oauth

Express.js connecting to Twitter


I am trying to Oauth authenticate with Twitter, using Express.js and Grant on my Windows 7 machine. When I run node app.js in command line I get the following:

The question is why doesn't MADE IT HERE also get outputted in the console. Also, what secret should I be putting in app.js where I currently have 'very secret'? Does this need to be my consumer secret or just any string?

I am using Xampp and when I want to run in my browser (Chrome) I direct to: http://dummy.com:3000/ and I get 'This webpage not available'. If I instead direct to http://localhost/xampp/phptest/lions/idk/run.html then I get a blank web page. Which should I be using?

I was trying to follow alongside this: https://scotch.io/tutorials/implement-oauth-into-your-express-koa-or-hapi-applications-using-grant#configure-express

Here are all of my files:

app.js

var express = require('express')
 , logger = require('morgan')
  , bodyParser = require('body-parser')
  , cookieParser = require('cookie-parser')
  , session = require('express-session');
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
var Grant = require('grant-express')
  , grant = new Grant(obj) ;

var app = express();
app.use(logger('dev'));
app.use(grant);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(session({
  name: 'grant', secret: 'very secret',
  saveUninitialized: true, resave: true
}));
app.get('/handle_twitter_callback', function (req, res) {
  console.log('MADE IT HERE');
  console.log(req.query);
  res.end(JSON.stringify(req.query, null, 2));
});

app.listen(3000, function() {
       //document.getElementById("holder").innerHTML="GOT HERE";

  console.log('Express server listening on port ' + 3000);

});

config.json

{ "server": {
    "protocol": "http",
    "host": "dummy.com:3000"

  },
  "twitter": 
  {
    "key": "myconsumerkey",
    "secret": "myconsumersecret",
    "callback": "/handle_twitter_callback"

    }
   } 

package.json

{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "bin": "./",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.13.2",
    "cookie-parser": "^1.3.5",
    "errorhandler": "^1.4.1",
    "express": "^4.13.1",
    "express-session": "^1.11.3",
    "fs": "0.0.2",
    "grant-express": "^3.3.3",
    "jade": "^1.11.0",
    "method-override": "^2.3.4",
    "morgan": "^1.6.1",
    "multer": "^0.1.8",
    "serve-favicon": "^2.3.0"
  }
}

run.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>run</title>
        <meta name="author" content="stephen" />
        <!-- Date: 2015-07-17 -->
    </head>
    <body>
    <script src="app.js"> </script>
    </body>
</html>

Solution

  • There's a few things to note here:

    1) Its important to note that node.js is JavaScript as/on a server. If you're using node.js, you don't need xampp. Xampp is a server usually for running php. Node is creating a server itself, so you don't need to use xampp at all.

    app.listen(3000, function() {
      console.log('Express server listening on port ' + 3000);
    });
    

    Is your app.js file telling your server to run on port 3000. Just hit localhost:3000 to view whatever page you're serving from your app.js file.

    2) If you're looking to print something out on your console, use console.log('something');, and you'll see it in the same console as your Express server... stuff. Note that you're using the server console, not your browser's console. It looks like you're trying with //document.getElementById("holder").innerHTML="GOT HERE"; to change stuff in the browser from you server file, which is probably not what you're looking to do. You'll need to include a file to be run client-side to manipulate dom stuff.

    3)

     app.get('/handle_twitter_callback', function (req, res) {
      console.log('MADE IT HERE');
      console.log(req.query);
      res.end(JSON.stringify(req.query, null, 2));
    });
    

    You'll need to head to localhost:3000/handle_twitter_callback to reach that, and I believe you'll see what you're looking for.

    I would suggest hitting a tutorial that explains node and express full through without anything extra to begin with, then trying OAuth stuff.