Express.js连接到Twitter

我正在尝试使用Express.js和Grant在Windows 7机器上使用Twitter进行Oauth身份validation。 当我在命令行中运行node app.js ,我得到以下内容:

问题是为什么不能在这里也输出控制台。 另外,我应该把哪个秘密放在app.js中,我目前有“非常秘密”? 这是否需要我的消费者的秘密或只是任何string?

我正在使用Xampp,当我想要在浏览器(Chrome)中运行时,我直接访问: http : //dummy.com : 3000/,并且得到“此网页不可用”。 如果我改为直接访问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。 如果您使用node.js,则不需要xampp。 Xampp是一个通常用于运行php的服务器。 节点正在创build一个服务器本身,所以你根本不需要使用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...相同的控制台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来达到目的,我相信你会看到你在找什么。

我会build议点击一个教程,解释节点和expression完整通过没有任何额外的开始,然后尝试OAuth的东西。