Twitter API request error while using node.js - node.js

I am trying to make a request to the Twitter API and respond with the JSON.
This is my code.
var http = require('http');
var request = require('request');
var url = require('url');
app.get('/twwets/:username', function(req, response) {
var username = req.params.username;
options = {
protocol: "http:",
host: 'api.twitter.com',
pathname: '/1/statuses/user_timeline.json',
query: { screen_name: username, count: 10}
}
var twitterUrl = url.format(options);
request(twitterUrl).pipe(response);
}).listen(8080);
And this is what I get when I run node app.js in my terminal:
module.js:340
throw err;
^
Error: Cannot find module 'require'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/geosh/node.js_practice/express_practice/app.js:2:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
I need to know what do I do wrong.
[My code may seem useless, but it's just for educational purposes. Please excuse any mistake.]
EDIT: I still get the same error after I saved and run the code in my shell.

Typo;
var request = require('require');
should most likely be
var request = require('request');
EDIT: You're also missing express, I suspect you'll want to add these lines;
var express = require('express');
var app = express();
...and npm install request express to get the modules installed.

Refer the following links may use
http://expressjs.com/
For Twitter Authentication
http://passportjs.org/

Related

How to pass API key authorization in header while consuming an API using Node Js?

I am working on consumption of API using Node Js. I am stuck at a point as I do not know how to consume an authorized API. Kindly tell me where did I go wrong?
Following is the code I wrote while doing this:
var express = require(express);
var app = express();
var http = require('http');
var myAPIKey= 'SharedAccessSignature sr=e91e0083-2c2b-47de-91bc-8c0b5f4fd3fd&sig=j9tCl4xKFjTpfNrHt%2BD4xQjVt15SxXSPM4bXj4sO6Zw%3D&skn=BMC004Connect&se=1620277640046'
var options = {
host:'bmc.azureiotcentral.com',
port: 80,
path:'/api/preview/devices',
method:'GET',
headers: {
"Authorization": "Basic" + myAPIKey
}
};
http.request(options, function(res){
var body ='';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var values = JSON.parse(body);
console.log(values);
});
}).end();
and I am getting the following error:
internal/modules/cjs/loader.js:983
throw err;
^
Error: Cannot find module 'express'
Require stack:
- E:\ReactNative\try\app.js
?[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)?[39m
?[90m at Function.Module._load (internal/modules/cjs/loader.js:862:27)?[39m
?[90m at Module.require (internal/modules/cjs/loader.js:1040:19)?[39m
?[90m at require (internal/modules/cjs/helpers.js:72:18)?[39m
at Object.<anonymous> (E:\ReactNative\try\app.js:1:15)
?[90m at Module._compile (internal/modules/cjs/loader.js:1151:30)?[39m
?[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)?[39m
?[90m at Module.load (internal/modules/cjs/loader.js:1000:32)?[39m
?[90m at Function.Module._load (internal/modules/cjs/loader.js:899:14)?[39m
?[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)?[39m {
code: ?[32m'MODULE_NOT_FOUND'?[39m,
requireStack: [ ?[32m'E:\\ReactNative\\try\\app.js'?[39m ]
}
First, Install express in you project
npm i express
You can check if it's installed by finding it in the packege.json and/or if its in the node_module dir
Second, Use string to import the modules.
var express = require('express');
This will take care of your error.
About the consume an authorized API. The header looks good but you need to add space between the Basic and the value.
headers: {
"Authorization": "Basic " + myAPIKey
}

ActiveDirectory Authentication using NODE for Windows SQL 2012

Am using node Active directory for validating users. Here is my node code:
const express = require('express');
const async = require('async');
const activedirectory = require('activedirectory');
var underscore = require('underscore');
const ldap = require('ldapjs');
const bunyan = require('bunyan');
const app = express();
app.get('/',(req,res) => {
var config = { url: 'ldap://10.100.1.10',
baseDN: 'dc=HBCAudit,dc=COM',
username: 'traveldesk.dev',
password: '$oftvision#123' }
var ad = new activedirectory(config);
});
//Port Number
const port = 7000;
//CORS Middleware
app.use(cors());
//Start Server
app.listen(port, () =>{
console.log('Server started on port '+ port);
});
But am getting error in command prompt like this:
c:\spitravel>node app.js
module.js:471
throw err;
^
Error: Cannot find module 'once'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (c:\spitravel\node_modules\ldapjs\lib\client\client.js
:8:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
I don't know why am getting this error. Am new to NODEJS.
Its due to depencies of ldapjs module, please run npm install ldapjs and recheck

Node.js error: Cannot find module 'firebase/storage'

I've been using Firebase Cloud Storage for my app, and so I required 'firebase/storage' in my server.js file, as shown below. The code was working fine last night, but when I tried "node server.js" this morning, I keep getting the module error:
Error: Cannot find module 'firebase/storage'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/home/ubuntu/workspace/app.js:10:15)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
Here is a snippet of the server.js code:
var express = require('express');
var logger = require('morgan')
var bodyParser = require('body-parser')
var app = express();
var path = require('path');
var formidable = require('formidable');
var fs = require('fs');
var admin = require('firebase-admin')
var storage = require('firebase/storage')
I've initialized in the fireabase app and made correct references to the firebase storage in my html and js, or else my code would not have worked last night. Why am I getting this error now?
You should use this lib to use storage.
npm install firebase
https://firebase.google.com/docs/storage/web/start

Cannot node app.js because 'Cannot read property 'MemoryStore' of undefined'

I'm using vagrant and virtualbox with precise32 for my setup. Installed Node.js v0.10 using:
curl -sL https://deb.nodesource.com/setup_0.10 | sudo bash -
sudo apt-get install -y nodejs
and i can't seem to sudo node the following:
var everyauth = require('everyauth');
var nconf = require('nconf');
var Recaptcha = require('recaptcha').Recaptcha;
var crypto = require('crypto');
var connect = require('connect');
var cookie = require('cookie');
var express = require('express');
var models = require('./models/');
var hash = require('./hash');
var config = require('./config');
/* load API keys */
nconf.env().file({file: 'settings.json'});
var session_store = new connect.session.MemoryStore();
I got the following error:
TypeError: Cannot read property 'MemoryStore' of undefined
at Object.<anonymous> (/vagrant/app/auth.js:16:40)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/vagrant/app/app.js:8:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:945:3
at node.js:945:3
I have to say that 3 years ago when I made the project, I simply did 'sudo npm install' and the tried to run and everythin was fine. I am thinking there is a incompatibility issue between js and the newly installed npm.
Change
var connect = require('connect');
...
var session_store = new connect.session.MemoryStore();
to
let app = express();
let session = require('express-session');
app.use(session({ ... });
Or install old version (2.x, 3.x) of Express

Can't run this nodejs code?

Im trying to run this simple code but I m getting error all the time...I really dont know what's the problem. It seems to me very Ok...? Thanks!
var http = require(http);
console.log('Starting');
var host = '127.0.0.1';
var port = 1337;
var server = http.createServer(function(request, response){
console.log('Receive request: '+ request.url);
response.writeHead(200, {"Content-type" : "text/plain"});
response.end("Hello world");
});
server.listen(port, host, function(){
console.log('Listening: ' + host + ':' + port);
});
Console error is this:
assert.js:98
throw new assert.AssertionError({
^
AssertionError: path must be a string: path must be a string
at Module.require (module.js:362:3)
at require (module.js:380:17)
at Object.<anonymous> (/Users/yoniPacheko/PhpstormProjects/angularJS/nodeJS/server.js:9:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
In your first line there are quotes missing around http:
var http = require('http');
In my case, I had
var http = require('http');
var path= require('path');
As soon as I deleted the the http line, it worked for me.So, i did leave only
var path= require('path');
I was working on express,mongodb,socket.io.

Resources