我正在玩 Node.js 并尝试发出 Twitter API 请求。
我正在使用 Express 生成的项目,所以我app.js
看起来像这样
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, url = require('url');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
index.js
像这样
exports.index = function(req, res){
var username = 'Axiol';
option = {
protocol: 'http:',
host: 'api.twitter.com',
pathname: '/1/statuses/user_timeline.json',
query: { screen_name: username, count: 10 }
}
var twitterUrl = url.format(options);
console.log(twitterUrl);
};
看起来很不错,但是,它总是给我这些错误
ReferenceError: url is not defined
at exports.index (/Users/Arnaud/Sites/badge/routes/index.js:17:20)
at callbacks (/Users/Arnaud/Sites/badge/node_modules/express/lib/router/index.js:161:37)
at param (/Users/Arnaud/Sites/badge/node_modules/express/lib/router/index.js:135:11)
at pass (/Users/Arnaud/Sites/badge/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/Users/Arnaud/Sites/badge/node_modules/express/lib/router/index.js:170:5)
at Object.router (/Users/Arnaud/Sites/badge/node_modules/express/lib/router/index.js:33:10)
at next (/Users/Arnaud/Sites/badge/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at Object.methodOverride [as handle] (/Users/Arnaud/Sites/badge/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:37:5)
at next (/Users/Arnaud/Sites/badge/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at multipart (/Users/Arnaud/Sites/badge/node_modules/express/node_modules/connect/lib/middleware/multipart.js:64:37)
看起来它找不到url
,但它在app.js
...中定义得很好
任何想法 ?