I am using node, express, and handlebars to try to display some tweets using the twitter api and the user's input. I am really new to node, so the chain of events has me confused. Essentially, I need some way to be able to call the **twits**, and pass in the user input as the query in the parameter. The way I am going about it now produces the error **unexpected token <** which is at <!DOCTYPE .
**App.js**: The function I am referring to is the second to last app.use
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var hbs = require('express-handlebars');
var twits = require('./twits');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.engine('hbs',hbs({extname: 'hbs',defaultLayout: 'layout',layoutsDir: __dirname + '/views/layouts/'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/test/:id',twits);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
**index.js**
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Twitter Sentiment Analysis', p: "How's the web feeling?" });
});
router.get('/test/:id', function(req, res, next) {
res.render('test',{output: req.params.id}); //consider passing twit, sentiment, config?
});
router.post('/search/',function(req,res, next) {
var id = req.body.id;
res.redirect('/test/' + id);
});
module.exports = router;
**twits.js:** using the twit module and sentiment module for basic sentiment analysis.
module.exports = function(query){
var Twit = require('twit');
var config = require('./views/config'); //Config just stores the keys
var sentiment = require('sentiment');
var T = new Twit(config);
var params = {
q: query,
count: 10,
type: 'recent',
exclude_replies: true,
retweeted: false,
favorite_count: 1
};
T.get('search/tweets', params, gotData);
function gotData(err, data, response) {
console.log(err);
//I know I need to change these from console.log to something else, but I am not sure what.
for(var i = 0; i<data.statuses.length;> console.log("Tweet " + (i + 1) + ":")
console.log("***********************")
console.log(data.statuses[i].text)
console.log(sentiment(data.statuses[i].text).score)
console.log("***********************")
}
}
};
**test.hbs** //the page where I want to display the tweets for the user. I havent begun stylizing it yet.
This is a test!
ID Passed: {{ output }}
<script type="text/javascript" src="app.js"></script>
<body>
</body>
**All views inherit from layout.hbs:**
<!doctype html>
<html lang = "en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link rel="stylesheet" href="http://s3.amazonaws.com/codecademy-content/courses/ltp/css/bootstrap.css">
<link rel="stylesheet" href="/stylesheets/style.css">
<link rel="stylesheet" href="/stylesheets/fonts.css">
<link rel="stylesheet" href="/stylesheets/animate.css">
</head>
<body>
{{{ body }}}
</body>
</html>
What I have tried:
I have tried inserting the twit functions inside of the HTML doc in question, wrapped with script tags.