Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to build an application that search for twitter hashtags and then displays them on a webpage. I'm using NodeJS for this. I've followed a tutorial and got it to work.

However, as I'm trying to learn, I've got some questions:

If I've understand correctly I need to use Express to serve the client side of my project (display the results on a webpage). Is this the right way to use NodeJS with an API or are there other ways?

In my application, I'm using hogan-express as template engine, do I need to use a template engine or can I use just normal html?

Thank you for your answers

What I have tried:

This is my project
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');
// create instance of var 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: 'dhooGTRTJS9kYnOWT19S6Pofj',
    consumer_secret: 'e7uK3EltqdS0JMuIK7OKdPuNrwpBajuLYq37okJ7plIQeRzPgQ',
    access_token: '492232617-HMmUUkWTfqgGBo2LKF4cIn1rRO7ujyNAUevgGrZ3',
    access_token_secret: 'RQjrqMMwNUkTWfK22uKx7tT5Ckk9ddQW3WNY1eo7FXnzy'
});

const 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);
    }
}
Posted
Updated 2-May-18 3:21am

1 solution

Quote:
If I've understand correctly I need to use Express to serve the client side of my project (display the results on a webpage). Is this the right way to use NodeJS with an API or are there other ways?
Oh, no no no, you are all wrong. Express is a server wrapper for Node.js, and provides you with the server-side programming services and functionality. In your own code, please check,
JavaScript
// Server is the Express and it is merely returning the HTML; not rendering it.
server.get('/', function(req, res) {
    res.render('index', {title: 'Twitter', data: theTweets});
});

The client-side library in your code might be hogan-express[^] and that will be responsible for generating dynamic HTML content and providing it. Still, we have not yet reached the client-side. The ruler of the client-side realm is JavaScript. I do not see any JavaScript library that you are injecting there.

But if you meant, to serve the requests: Then yes, you are right, Express is used to serve the requests in Node.js as it provides a flexible way to manage your server. :-)
Quote:
Is this the right way to use NodeJS with an API or are there other ways?
Yes, Express is a great library to serve most of the purposes that you have in mind. Now understand this, that you are only calling the API to get some result. In Node.js the preferred result from an API is the JSON document, since Node.js has native support for that.

The Twit library would definitely have its own documentation that you can reach and understand to further proceed.

Quote:
In my application, I'm using hogan-express as template engine, do I need to use a template engine or can I use just normal html?
You are a bit wrong in this part too, sorry. hogan-express has been long deprecated by its own manager, developer. Check the link I attached for hogan-express. I can recommend using either Pug, or EJX, there are many other template libraries, but these are somewhat bearable. :D

Lastly, simple HTML files will not serve you any benefit, since you will need to use a template nonetheless to show the tweets on the page, otherwise, a static page won't contain much information — unless you plan to create a new HTML page for each request.

Twit package on NPM[^]

You might be interested in my Node.js sample, that I created for beginners to try and learn, it demonstrates basics of Node.js, Express, Pug and a few more technologies that you can find on the repository: GitHub - afzaal-ahmad-zeeshan/nodejs-dockerized: Dockerized Node.js application.[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900