I'm playing around with Node.js and trying to make a Twitter API request.
I'm using an Express generated project, so my app.js looks like that
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'));
});
And the index.js like this
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);
};
Seems pretty fine, but, it always return me these errors
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)
Looks like it can't find url, but it's well defined in app.js...
Any idea ?
You need to do var url = require('url') in your routes/index.js file as well. I assume this is because of the nodejs module system. Each module is isolated, and the only thing visible from the outside are the things that you add to exports (and globals, obviously). So local variables in a module is not available to other modules.
The reason you are getting the error is because the "url" module was not imported into your index.js module.
Every module in node.js is encapsulated and it does not expose anything to the outside world, unless you specify it through your exports, as you are doing with your exports.index = function(...) { ... }
However, if you do that it will not work, since Node's URL module is not intended to make HTTP requests, but for dealing with URLs. As per Node's doc:
"This module has utilities for URL resolution and parsing". See http://nodejs.org/api/url.html
If you want to make an HTTP request to Twitter, you can use a Node module called "request" which simplifies things a little bit. See https://github.com/mikeal/request
Hope this helps.