This code works fine but it seems that it retrieve any tweet not only the people I follow, I want to get my timeline tweets
what should I do ?
var open = require('open');
var sys = require('sys');
var spawn = require('child_process').spawn
var OAuth= require('oauth').OAuth;
var express = require("express");
var util = require('./util');
var app = express();
var oa = new OAuth(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"NeFpQZNDapDtFJnUXDe0Q",
"mXf7AKYJWIdiToKuSgsTm2me0GBA1aNn39uskmEoOM",
"1.0",
"http://127.0.0.1:8080/twitterCallback",
"HMAC-SHA1"
);
var oauthtoken ,oauthtokensecret;
{
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
if (error) {
console.log(error);
res.send("yeah no. didn't work.")
}
else {
oauthtoken = oauth_token;
oauthtokensecret = oauth_token_secret;
open('http://twitter.com/oauth/authenticate?oauth_token='+oauthtoken);
}
});
}
var http = require('http');
var fs = require('fs');
function readFile(fileName,response){
fs.readFile(fileName, function (err,data) {
if (err) {
return console.log(err);
}
response.end(data);
});
}
var server = http.createServer(function(req, res) {
if(req.url == '/favicon.ico'){
res.end('Hello Http');
}else if(req.url.indexOf('twitterCallback') != -1){
res.writeHead(200);
res.end('Hello Http');
var url = require('url');
var url_parts = url.parse(req.url, true);
var query = url_parts.query;
var oauth_verifier = query.oauth_verifier;
console.log(oauth_verifier);
oa.getOAuthAccessToken(oauthtoken,oauthtokensecret,query.oauth_verifier, function(error, oauth_access_token, oauth_access_token_secret, results)
{
if (error){
console.log(error);
} else {
//console.log("oauth_access_token = " + oauth_access_token);
//console.log("oauth_access_token_secret = " + oauth_access_token_secret);
}
var twitter = require('ntwitter');
var twit = new twitter({
consumer_key: 'NeFpQZNDapDtFJnUXDe0Q',
consumer_secret: 'mXf7AKYJWIdiToKuSgsTm2me0GBA1aNn39uskmEoOM',
access_token_key: oauth_access_token,
access_token_secret: oauth_access_token_secret
});
twit.stream('statuses/sample', function(stream) {
stream.on('data', function (data) {
console.log(data.text);
});
});
}
);
}else{
readFile(req.url.substr(1));
}
});
server.listen(8080);
Use the ntwitter library : https://github.com/AvianFlu/ntwitter
And to consume api :
var twitter = require('ntwitter');
var twit = new twitter({
consumer_key: 'Twitter',
consumer_secret: 'API',
access_token_key: 'keys',
access_token_secret: 'go here'
});
twit.stream('user', {track:'nodejs'}, function(stream) {
stream.on('data', function (data) {
console.log(data);
});
stream.on('end', function (response) {
// Handle a disconnection
});
stream.on('destroy', function (response) {
// Handle a 'silent' disconnection from Twitter, no end/error event fired
});
// Disconnect stream after five seconds
setTimeout(stream.destroy, 5000);
});
Related
Im trying to host a election website.
My Current Method explained:
There is server.js file with serves the public file. If its a POST request(sent by frontend.js to send data to server) edits the poll.json file based on the data sent.
I have a public file having index.html, frontend.js, poll.json where poll.json stores all the data.
My current code work all well in my localhost.
But when running it in Heroku I get a error in the POST request line saying 'POST http://localhost:53390/ net::ERR_CONNECTION_REFUSED1
*********.herokuapp.com/:1 Uncaught (in promise) TypeError: Failed to fetch'
My server.js code:
const createServer = require('http').createServer;
const express = require('express');
const app = express();
let pollData = require(__dirname+'/public/poll.json');
const fs = require('fs');
var all_usn_no=[];
const listening_port=process.env.PORT || 4000;
function get_roll_and_usn(pollData){
for(var i=0;i<=pollData.students.length-1;i++){
//all_roll_no.push(pollData.students[i][0]);
all_usn_no.push(pollData.students[i][1]);
}
}
function roll_to_row(in_usn){
get_roll_and_usn(pollData)
return all_usn_no.indexOf(in_usn);
}
function write_vote(votes){
var checking_row=roll_to_row(votes[1]);
pollData.students[checking_row]=votes;
fs.writeFile(__dirname+'/public/poll.json', JSON.stringify(pollData), (err) => {
if (err) throw err;
console.log('Data written to file');
});
}
write_vote([listening_port,0]);
app.use(express.static(__dirname+'/public'));
app.get('/', (req, res) => {
res.sendFile(__dirname);// + '/index.html');
});
app.post('/', (req, res) => {
let body = '';
req.on('data', data => body += data)
req.on('end', () => {
res.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET',
});
body=body.split(",");
console.log(body.toString());
write_vote(body);
res.end(`{ "response": "${body ? body : 'No body sent' }" }`);
})
});
app.listen(listening_port, () => {
console.log('Example app listening at http://localhost:'+listening_port)
});
my frontend.js program:
var roll='';
var client_ip='';
var usn='';
var date='';
const all_roll_no=[]
const all_usn_no=[]
var port='';
function check(roll,usn){
var data='';
const url='poll.json';
const Http = new XMLHttpRequest();
Http.open("GET", url);
Http.send();
Http.onload = () =>{
data=JSON.parse(Http.response);
get_roll_and_usn(data);
check_validity(roll,usn,data);
}
}
function get_roll_and_usn(pollData){
for(var i=0;i<=pollData.students.length-1;i++){
//all_roll_no.push(pollData.students[i][0]);
all_usn_no.push(pollData.students[i][1]);
}
}
function usn_to_row(in_usn){
return all_usn_no.indexOf(in_usn);
}
function check_validity(checking_roll,checking_usn,data){
var checking_row=usn_to_row(checking_usn);
port=data.students[0][0];
//if(all_roll_no.indexOf(checking_roll)>=0 && checking_usn==all_usn_no[all_roll_no.indexOf(checking_roll)] && data.students[checking_row][2]==0){
if(all_usn_no.indexOf(checking_usn)>=0 && data.students[checking_row][2]==0){
//console.log("valid");
document.getElementById("page_2").style.display = "none";
document.getElementById("page_3").style.display = "block";
fetch('https://api.ipify.org/?format=json').then(results=> results.json()).then(data => client_ip=data.ip);
}
else{
alert("You cannot vote/ You have already voted")
//console.log('invalid');
}
}
document.getElementById("startbutton").onclick = function(){
roll = document.getElementById("roll_no").value;
usn = document.getElementById("usn_no").value;
date = Date();
check(roll,usn);
}
document.getElementById("next").onclick = function() {
document.getElementById("page_1").style.display = "none";
document.getElementById("page_2").style.display = "block";
}
document.getElementById("finish").onclick = function() {
var splb=[document.getElementById("splb1").checked,document.getElementById("splb2").checked,document.getElementById("splb3").checked,document.getElementById("splb4").checked,document.getElementById("splb5").checked,document.getElementById("splb6").checked,document.getElementById("splb7").checked];
var splg=[document.getElementById("splg1").checked,document.getElementById("splg2").checked,document.getElementById("splg3").checked,document.getElementById("splg4").checked,document.getElementById("splg5").checked,document.getElementById("splg6").checked,document.getElementById("splg7").checked];
var asplb=[document.getElementById("asplb1").checked,document.getElementById("asplb2").checked,document.getElementById("asplb3").checked,document.getElementById("asplb4").checked,document.getElementById("asplb5").checked,document.getElementById("asplb6").checked,document.getElementById("asplb7").checked];
var asplg=[document.getElementById("asplg1").checked,document.getElementById("asplg2").checked,document.getElementById("asplg3").checked,document.getElementById("asplg4").checked,document.getElementById("asplg5").checked,document.getElementById("asplg6").checked,document.getElementById("asplg7").checked];
var csb=[document.getElementById("csb1").checked,document.getElementById("csb2").checked,document.getElementById("csb3").checked,document.getElementById("csb4").checked,document.getElementById("csb5").checked,document.getElementById("csb6").checked,document.getElementById("csb7").checked];
var csg=[document.getElementById("csg1").checked,document.getElementById("csg2").checked,document.getElementById("csg3").checked,document.getElementById("csg4").checked,document.getElementById("csg5").checked,document.getElementById("csg6").checked,document.getElementById("csg7").checked];
var acsb=[document.getElementById("acsb1").checked,document.getElementById("acsb2").checked,document.getElementById("acsb3").checked,document.getElementById("acsb4").checked,document.getElementById("acsb5").checked,document.getElementById("acsb6").checked,document.getElementById("acsb7").checked];
var acsg=[document.getElementById("acsg1").checked,document.getElementById("acsg2").checked,document.getElementById("acsg3").checked,document.getElementById("acsg4").checked,document.getElementById("acsg5").checked,document.getElementById("acsg6").checked,document.getElementById("acsg7").checked];
var scb=[document.getElementById("scb1").checked,document.getElementById("scb2").checked,document.getElementById("scb3").checked,document.getElementById("scb4").checked,document.getElementById("scb5").checked,document.getElementById("scb6").checked,document.getElementById("scb7").checked];
var scg=[document.getElementById("scg1").checked,document.getElementById("scg2").checked,document.getElementById("scg3").checked,document.getElementById("scg4").checked,document.getElementById("scg5").checked,document.getElementById("scg6").checked,document.getElementById("scg7").checked];
var ascb=[document.getElementById("ascb1").checked,document.getElementById("ascb2").checked,document.getElementById("ascb3").checked,document.getElementById("ascb4").checked,document.getElementById("ascb5").checked,document.getElementById("ascb6").checked,document.getElementById("ascb7").checked];
var ascg=[document.getElementById("ascg1").checked,document.getElementById("ascg2").checked,document.getElementById("ascg3").checked,document.getElementById("ascg4").checked,document.getElementById("ascg5").checked,document.getElementById("ascg6").checked,document.getElementById("ascg7").checked];
var vote=[String(splb.indexOf(true)),String(splg.indexOf(true)),String(asplb.indexOf(true)),String(asplg.indexOf(true)),String(csb.indexOf(true)),String(csg.indexOf(true)),String(acsb.indexOf(true)),String(acsg.indexOf(true)),String(scb.indexOf(true)),String(scg.indexOf(true)),String(ascb.indexOf(true)),String(ascg.indexOf(true))]
var update=[String(roll),String(usn),"1",String(client_ip),String(date)].concat(vote);
if (update.indexOf("-1")<0){
alert("Pls vote for all posts")
}
else{
document.getElementById("page_1").style.display = "none";
document.getElementById("page_2").style.display = "none";
document.getElementById("page_3").style.display = "none";
fetch('http://localhost:'+port,{method:'POST',body:update}).then(results=> results.json()).then(console.log);
//console.log(update);
alert("Your vote has been registered")
}
}
You can just ignore the function as they are just to process the data and do necessary funtions.
Main problem: PORT request from frontend.js to server.js sending data to edit the poll.json file return error.
Thanks in advance.
Try using const PORT = process.env.PORT || 3333;
with
app.listen(PORT, () => {
console.log(`Server is starting on ${PORT}`);
})
I have 2 endpoints in Node.js index.js file.
First one is working well and it turns 200 OK. But the second one is not working, it turns 404 Error. I could not find the reason.
First endpoint is /AddArac. It works well and adds data to MongoDB database
Second endpoint is /AddMesaj. It doesn't work. Both endpoints have the same logic.
My nodejs file is here:
`
var mongodb = require('mongodb');
var objectID = mongodb.ObjectID;
var express = require('express');
var bodyparser = require('body-parser');
var app = express();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
var mongoClient = mongodb.MongoClient;
var url = 'mongodb://localhost:27017'
mongoClient.connect(url, { useNewUrlParser: true }, function (err, client) {
if (err) {
console.log('Connection Fail', err);
}
else {
var db = client.db('basaranSMSDev');
//Add arac
app.post('/AddArac', (request, response, next) => {
var data = request.body;
var name = data.name;
var plaka = data.plaka;
var tel = data.tel;
var json = { 'name': name, 'plaka': plaka, 'tel': tel };
db.collection('Araclar').find({ 'plaka': plaka }).count(function (error, number) {
if (number > 0) {
response.json('Araç daha önce kaydedilmiş.');
}
else {
db.collection('Araclar').insertOne(json, function (error, res) {
if (err) {
response.json('Araç kaydında hata oluştu tekrar deneyiniz.');
console.log('Arac eklenemedi', err);
}
else {
response.json('Araç eklendi');
console.log('Arac eklendi.');
}
});
}
});
});
//Araç listesi getirme
//add mesaj
app.post('AddMesaj', (request, response, next) => {
var data_mes = request.body;
var plaka = data_mes.plaka;
var mtext = data_mes.text;
var driver = data_mes.driver;
var date = data_mes.tarih;
var json = { 'plaka': plaka, 'text': mtext, 'driver': driver, 'date': date };
db.collection('Mesajlar').insertOne(json, function (error, res) {
if (err) {
response.json('Mesaj gönderiminde hata oluştu tekrar deneyiniz.');
console.log('Mesaj eklenemedi', err);
}
else {
response.json('Mesaj gönderildi');
console.log('Mesaj eklendi.');
}
});
});
app.listen(3000, () => {
console.log('Connection ok');
});
//Mesaj listesi getirme
}
}
);
`
Change to:
app.post('/AddMesaj', (request, response, next) => {
i have code to make pdf and succeeded in downloading and opening it, but i want to send pdf to my server on node js, and i have made app.post on server but i can't make pdf become base64 and save it on server
in frontend
<script type="text/javascript">
function genPDF() {
html2canvas(document.getElementById('testDiv')).then(function (canvas) {
var img = canvas.toDataURL('image/png');
var doc = new jsPDF('landscape');
doc.addImage(img, 'png', 10, 10);
var temp = doc.save('test.pdf');
var post = new XMLHttpRequest();
post.open("POST", "/receive");
post.send(temp);
}
</script>
Download PDF
in server
app.post('/receive', function (request, respond) {
var body = '';
var filePath = './static' + '/document/Document.pdf';
//
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
var data = body.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
fs.writeFile(filePath, buf, function (err) {
if (err) throw err
respond.end();
});
});
});
how to send var temp = doc.save('test.pdf'); server and generate pdf to base64?
Use the below code this will help you.
IN FE
<script type = "text/javascript">
function genPDF() {
html2canvas(document.getElementById('testDiv')).then(function (canvas) {
var img = canvas.toDataURL('image/png');
var doc = new jsPDF('landscape');
doc.addImage(img, 'png', 10, 10);
var temp = doc.save('test.pdf');
var data = new FormData();
data.append("pdf_file", temp);
var post = new XMLHttpRequest();
post.open("POST", "/receive");
post.send(data);
}
</script>
<a href = "javascript:genPDF()" > Download PDF </a>
IN BE
const fs = require('fs');
const multipartMiddleware = require('connect-multiparty')();
const express = require('express');
const app = express();
const port = 8000;
const filePath = './static' + '/document/Document.pdf';
app.post('/', multipartMiddleware, (request, response) => {
fs.readFile(request.files.pdf_file.path, (err, data) => {
fs.writeFile(filePath, data, function (err) {
if (err) throw err;
response.send('Done')
});
})
})
app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
});
We have requirement where we need to write a node application which can read URL of image from database (approx more than million). Use image-size npm package to retrieve image meta data like height, width. Here should be an API which can list out result.
I am able to console log data but when i convert it to API, i need to chunk data so it can start appearing on browser and i'm unable to do that and need help. Here is my code
var express = require('express');
var url = require('url');
var http = require('http');
var sizeOf = require('image-size');
const sql = require('mssql');
var app = express();
var port = process.env.PORT || 3000;
const hostname = 'localhost';
var config1 = {
user: '*********',
password: '*********',
server: '*********',
database: '*******',
port: 1433,
debug: true,
options: {
encrypt: false // Use this if you're on Windows Azure
}
};
app.get('/', function(req, res){
//res.writeHead(200, { 'Content-Type': 'application/json' });
var finalResult = [];
sql.close();
sql.connect(config1, function (err) {
if (err) console.log(err);
const request = new sql.Request()
var myQuery = `select imagename from media`;
request.stream = true;
request.query(myQuery);
request.on('row', row => {
//console.log('Image : ' + row.ImageUrl);
if (row.ImageUrl != ''){
if (row.ImageUrl.indexOf('http') < 0)
row.ImageUrl = "http:" + row.ImageUrl;
var options = url.parse(row.ImageUrl);
http.get(options, function (response) {
if (response.statusCode == 200)
{
var chunks = [];
response.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function() {
var buffer = Buffer.concat(chunks);
//console.log(options.href);
//console.log(sizeOf(buffer).height);
var result = {};
result.MaskUrl = row.MaskUrl;
result.ImageUrl = options.href;
result.Height = sizeOf(buffer).height;
result.Width = sizeOf(buffer).width;
result.statusCode = 200;
finalResult.push(result);
//console.log(result);
console.log(finalResult);
res.write(result, function(){
res.end();
});
});
}
else
{
var result = {};
result.MaskUrl = row.MaskUrl;
result.ImageUrl = options.href;
result.Height = 0;
result.Width = 0;
result.statusCode = response.statusCode;
finalResult.push(result);
console.log(result);
res.write(result, function(){
res.end();
});
}
});
}
})
request.on('error', err => {
console.log ('Error for ' + row.ImageUrl );
})
request.on('done', err => {
console.log('Last Time' + finalResult.length);
})
// request.query(myQuery,(err,result) =>{
// console.log(result);
// });
});
console.log('Last Time' + finalResult.length);
res.send(finalResult);
});
app.listen(port, hostname, function(){
console.log('ImageSize running on PORT: ' + port);
});
I tried res.write, res.end without any success.
The probable reason for your problem is that here:
res.write(result, function(){
res.end();
});
You end and close the request just after the first image is read.
I would rewrite the code a little and use some functional framework, like scramjet, to stream the data straight from the DB. As Nicholas pointed out it's not super easy to run your code so I'm writing blindly - but if you fix any of my obvious error this should just work:
First:
npm install scramjet JSONStream node-fetch
Next, try this code:
var express = require('express');
var sizeOf = require('image-size');
const sql = require('mssql');
var app = express();
var port = process.env.PORT || 3000;
const hostname = 'localhost';
const {DataStream} = require('scramjet');
const fetch = require('node-fetch');
const JSONStream = require('JSONStream');
var config1 = {
user: '*********',
password: '*********',
server: '*********',
database: '*******',
port: 1433,
debug: true,
options: {
encrypt: false // Use this if you're on Windows Azure
}
};
app.get('/', function(req, res, next){
// you should consider not doing these two lines on each request,
// but I don't want to mess you code...
sql.close();
sql.connect(config1, function (err) {
if (err) next(err);
res.writeHead(200, { 'Content-Type': 'application/json' });
const request = new sql.Request();
var myQuery = `select imagename from media`;
request.stream = true;
request.query(myQuery);
const stream = new DataStream();
request.on('row', row => stream.write(row));
stream.filter(
row => row.ImageUrl !== ''
)
.map(
async row => {
if (row.ImageUrl.indexOf('http') !== 0) // url must start with http.
row.ImageUrl = "http:" + row.ImageUrl;
const response = await fetch(row.ImageUrl);
let size = {width:0, height:0};
if (response.status === 200) {
const buffer = await response.buffer();
size = sizeOf(buffer);
}
return {
MaskUrl: row.MaskUrl,
ImageUrl: row.ImageUrl,
Height: size.height,
Width: size.width,
statusCode: response.status
};
}
)
.pipe(
JSONStream.stringify()
).pipe(
res
);
request.on('error', () => {
res.writeHead(500, { 'Content-Type': 'application/json' });
stream.end("{error:true}");
});
request.on('done', () => stream.end());
});
});
app.listen(port, hostname, function(){
console.log('ImageSize running on PORT: ' + port);
});
I am trying to make a server and client program just on localhost to be able to upload and list files from the server. I have the code to upload files to the server but for some reason after the server writes the file from the client it has less bytes than the original file.
Here is the client code:
var net = require('net');
var fs = require('fs');
var port='3000';
var host='localhost';
var path=__dirname;
var fileUploadDir = __dirname+'/uploadfolder/';
process.stdin.resume();
var socket = net.connect(port,host);
var stdin = process.stdin;
var stdout = process.stdout;
stdout.on('data', function(d){
console.log("d: "+d);
});
socket.on('connect',function(){
console.log("connected to server");
//stdin.pipe(stdout);
stdin.pipe(socket, {end: false});
});
socket.on('data', function(data){
var b = new Buffer(data, "utf8");
var bStr = b.toString();
if(bStr=="okupload"){
console.log("uploading file");
folderFilesUpload(socket);
}else{
console.log(bStr);
}
});
socket.on('end', function(){
socket = net.connect(port,host);
console.log('socket ended');
});
function folderFilesUpload(socket){
fs.readdir(fileUploadDir, function(err, results){
if(err)
console.log("upload to upload files");
else{
if(results.length > 0){
for(var r in results){
var fullPath = fileUploadDir+results[r];
var fileStream = fs.createReadStream(fullPath);
fileStream.on('open', function(){
fileStream.pipe(socket);
});
}
}else{
console.log("no files to upload");
}
}
});
}
Server Code:
var net=require('net');
var fs=require('fs');
var host ='localhost';
var port='3000';
var outputDir = __dirname+'/outputdir/';
var uploadState = false;
var server = net.createServer(function(conn){
console.log('server connected');
conn.on('data', function(data) {
if(data == "l\n" && !uploadState){
listFiles(conn);
}
if(data == "u\n"){
console.log("switching to upload file state");
uploadState=true;
conn.write("okupload");
}
if(uploadState){
console.log("data");
//console.log("data: "+data);
var file = fs.createWriteStream(outputDir+'temp');
conn.pipe(file);
}
});
conn.on('end', function(){
uploadState = false;
console.log('data has ended');
});
});
server.listen(port,host, function(){
server.on('connection', function(){
console.log('connection made');
});
});
function listFiles(conn){
console.log("listing files");
fs.readdir(outputDir, function(err, results){
if(err){
var b = new Buffer("error listing files", "utf8");
conn.write(b);
}else{
var output = "files: \n";
if(results !== null && results.length >0){
for(var r in results){
output += results[r]+"\n";
}
}else{
output += "no files.\n";
}
var b = new Buffer(output, "utf8");
conn.write(b);
}
});
}