I have the following problem, I try to call a dependency in this way but it tells me that the word require is not defined, I would like to know if it can only be used on the express server or on any server that is used on node.
To access the Twitter API, do not try to use it from the browser directly through browserfy or similar, as you should expose your passwords, which are secret. It is discouraged by them .
Create a server application that queries the API and exposes it to an access point to "bridge" your server to actually do the query, so your keys aren't compromised.
// server.js
var express = require('express');
var Twitter = require('twitter');
var twclient = new Twitter({
consumer_key: '', // debes poner los datos correctos en estos campos
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
// creamos la applicacion con express
var app = express();
// configuramos la carpeta 'public' como una carpeta de contenido estatico, html, css, etc.
app.use(express.static('public'));
app.get('/ultimotweet/:user', function(req, res){
// preparamos los datos del usuario a consultar
var usuario = {
// en screen_name, pones el nombre de usuario sin la arroba.
// req.params.user, el valor que viene en la solicitud ej: /ultimotweet/nombreuser => nombreuser
screen_name: req.params.user,
// la cantidad de mensajes a obtener
count: 1,
// solo mensajes propios (no re-tweets)
include_rts: false
};
twclient.get('statuses/user_timeline', usuario, function(error, tweets, response){
if(error) throw error; // se produjo un error, manejar aquí
if (tweets.length) {
// como hay tweets, con res.send enviamos la respuesta al navedador.
res.send(tweets[0].text);
} else {
// como no hay tweets mandamos un mensaje que lo explique.
res.send('El usuario no ha twiteado aun!');
}
});
});
app.listen(3000);
Then make a client application similar to this:
Using ajax, you query the access point created in the previous application and get the information you are looking for.
<input id="user" type="text" placeholder="ingresa el usario de sin la @">
<button id="pedir">Obtener</button>
<div id="destino_tweet">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#pedir').click(function() {
$.ajax({
url: '/ultimotweet/' + $('#user').val()
}).done(function( data ) {
$('#destino_tweet').text(data);
});
});
});
</script>
I put down a repository on GitHub so you can test this concept, with instructions to get you running.
The method require();is only used in node.js on the server side so that it works in the browser on the client side use a library like require.js or browserfy
To access the Twitter API, do not try to use it from the browser directly through browserfy or similar, as you should expose your passwords, which are secret. It is discouraged by them .
Create a server application that queries the API and exposes it to an access point to "bridge" your server to actually do the query, so your keys aren't compromised.
Then make a client application similar to this:
Using ajax, you query the access point created in the previous application and get the information you are looking for.
I put down a repository on GitHub so you can test this concept, with instructions to get you running.
https://github.com/rnrneverdies/nodetwitter
The method
require();
is only used in node.js on the server side so that it works in the browser on the client side use a library like require.js or browserfyIf it's on the client side, don't use it
require()
but link the module in your html file with<script src='./twitter'></script>