I'm trying to learn ExpressJS and NodeJS to use with the Twitter API. This is my setup so far:
package.json
{
"name": "twitter-express",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"hogan-express": "^0.5.2",
"twit": "^2.2.9"
}
}
server.js
var express = require('express');
var server = express();
server.set('port', process.env.PORT || 3003);
server.engine('html', require('hogan-express'));
server.set('view engine', 'html');
server.get('/', function(req, res) {
res.render('index', {title: 'Twitter', data: theTweets});
});
server.listen(server.get('port'), function() {
console.log('Listening on port:' + server.get('port'));
});
var Twit = require('twit');
var theTweets = [];
var T = new Twit({
consumer_key: '',
consumer_secret: '',
access_token: '',
access_token_secret: ''
});
var params = {
q: '#nodejs',
count: 10,
result_type: 'recent',
lang: 'en'
}
T.get('search/tweets', params, getData);
function getData(err, data, response) {
var tweets = data.statuses;
for (var i = 0; i < tweets.length; i++) {
theTweets.push(tweets[i].text);
}
}
I'm using hogan-express as template engine as I followed a tutorial. With this setup everything works and the tweets are displayed on the webpage. However, as I'm trying to learn this I have some questions about Express and Node:
If I've understand correctly, I need to use Express to serve the client side of my application (to display tweets on a webpage), is this the best way for a Node application or can I do it in some other way?
Do I have to use a template engine or can I use it just with html? In that case, how can I use it without a template engine? Thank you.