0

我是 Node JS 的初学者,我正在尝试使用它在 Twitter 上进行连接。我在 E:/ 目录中有一个 js 文件,但我不知道是否应该创建一个模块文件夹或使用以下代码

    var express = require('express');
var sys = require('sys');
var oauth = require('oauth');

var app = express.createServer();

var _twitterConsumerKey = "****SECRET****";
var _twitterConsumerSecret = "****SECRET****";

function consumer() {
  return new oauth.OAuth(
    "https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token", 
    _twitterConsumerKey, _twitterConsumerSecret, "1.0A", "http://badgestar.com/sessions/callback", "HMAC-SHA1");   
}

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  app.use(express.logger());
  app.use(express.cookieDecoder());
  app.use(express.session());
});

app.dynamicHelpers({
  session: function(req, res){
    return req.session;
  }
});

app.get('/', function(req, res){
  res.send('Hello World');
});

app.get('/sessions/connect', function(req, res){
  consumer().getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
    if (error) {
      res.send("Error getting OAuth request token : " + sys.inspect(error), 500);
    } else {  
      req.session.oauthRequestToken = oauthToken;
      req.session.oauthRequestTokenSecret = oauthTokenSecret;
      res.redirect("https://twitter.com/oauth/authorize?oauth_token="+req.session.oauthRequestToken);      
    }
  });
});

app.get('/sessions/callback', function(req, res){
  sys.puts(">>"+req.session.oauthRequestToken);
  sys.puts(">>"+req.session.oauthRequestTokenSecret);
  sys.puts(">>"+req.query.oauth_verifier);
  consumer().getOAuthAccessToken(req.session.oauthRequestToken, req.session.oauthRequestTokenSecret, req.query.oauth_verifier, function(error, oauthAccessToken, oauthAccessTokenSecret, results) {
    if (error) {
      res.send("Error getting OAuth access token : " + sys.inspect(error) + "["+oauthAccessToken+"]"+ "["+oauthAccessTokenSecret+"]"+ "["+sys.inspect(results)+"]", 500);
    } else {
      req.session.oauthAccessToken = oauthAccessToken;
      req.session.oauthAccessTokenSecret = oauthAccessTokenSecret;
      // Right here is where we would write out some nice user stuff
      consumer.get("http://twitter.com/account/verify_credentials.json", req.session.oauthAccessToken, req.session.oauthAccessTokenSecret, function (error, data, response) {
        if (error) {
          res.send("Error getting twitter screen name : " + sys.inspect(error), 500);
        } else {
          req.session.twitterScreenName = data["screen_name"];    
          res.send('You are signed in: ' + req.session.twitterScreenName)
        }  
      });  
    }
  });
});

app.listen(parseInt(process.env.PORT || 80));

但它在运行时给了我这个错误说:

module.js:340
    throw err;
          ^
Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (E:\hello.js:1:77)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:47
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
4

3 回答 3

3

看起来快递没有正确安装。在您的项目目录中尝试sudo npm install express它应该创建一个 node_modules 文件夹并将 express 模块添加到其中。

进一步建议将护照中间件用于您的身份验证策略。

于 2013-08-12T20:30:12.560 回答
0

您的 Express 安装未正确安装,或者所需的安装不正确。

使用链接

http://passportjs.org/guide/twitter/

在您的要求示例链接上使用以下代码

https://github.com/jaredhanson/passport-twitter

于 2013-08-20T05:18:44.213 回答
0

你需要安装快递。命令是:npm install express。然后你会看到 node_module 文件夹将被创建并且 express 模块将在 node_module 中可用。

于 2015-06-27T09:32:58.227 回答