Express.js 连接到 Twitter

Express.js connecting to Twitter

我正在尝试在我的 Windows 7 机器上使用 Express.js 和 Grant 通过 Twitter 进行 Oauth 身份验证。当我在命令行中 运行 node app.js 时,我得到以下信息:

问题是为什么 MADE IT HERE 也没有在控制台中输出。 此外,我应该在 app.js 中放入什么秘密,而我目前拥有 'very secret'?这需要是我的消费者机密还是任何字符串?

我正在使用 Xampp,当我想在浏览器中 运行 (Chrome) 时,我直接转到:http://dummy.com:3000/ and I get 'This webpage not available'. If I instead direct to http://localhost/xampp/phptest/lions/idk/run.html 然后我得到一个空白网页。我应该使用哪个?

我试着跟着这个:https://scotch.io/tutorials/implement-oauth-into-your-express-koa-or-hapi-applications-using-grant#configure-express

这是我的所有文件:

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>

这里有几点需要注意:

1) 重要的是要注意 node.js 是 JavaScript as/on 一个服务器。如果您使用 node.js,则不需要 xampp。 Xampp 是通常用于 运行ning php 的服务器。 Node 正在创建自己的服务器,因此您根本不需要使用 xampp。

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

是您的 app.js 文件告诉您的服务器 运行 在端口 3000 上。只需点击 localhost:3000 即可查看您从 app.js 文件提供的任何页面。

2) 如果您希望在您的控制台上打印一些内容,请使用 console.log('something');,您将在与 Express server... 内容相同的控制台中看到它。请注意,您使用的是服务器控制台,而不是浏览器的控制台。看起来您正在尝试使用 //document.getElementById("holder").innerHTML="GOT HERE"; 从您的服务器文件更改浏览器中的内容,这可能不是您想要做的。您需要包含一个文件作为 运行 客户端来操作 dom 内容。

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));
});

您需要前往 localhost:3000/handle_twitter_callback 才能到达那里,我相信您会找到想要的东西。

我建议点击一个教程来解释节点并在没有任何额外内容的情况下完整地表达,然后尝试 OAuth 的东西。