Parsing JSON file using Lambda - json

My main objective is to parse a JSON file and write the content of the JSON file to a scrollable list in the browser. I created a JSON file and stored it in an S3 bucket. I then created the following Lambda function:
// ----receive function----v
function get_json(url, callback) {
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
// call function ----v
callback(response);
});
});
}
var mydata = get_json("https://s3.amazonaws.com/scrollablelist/scrollableList.json", function (resp) {
console.log(resp);
});
I get the following error:
{
"errorMessage": "http is not defined",
"errorType": "ReferenceError",
"stackTrace": [
"Object.<anonymous> (/var/task/index.js:20:18)",
"Module._compile (module.js:570:32)",
"Object.Module._extensions..js (module.js:579:10)",
"Module.load (module.js:487:32)",
"tryModuleLoad (module.js:446:12)",
"Function.Module._load (module.js:438:3)",
"Module.require (module.js:497:17)",
"require (internal/module.js:20:19)"
]
}
The Log Output says this:
START RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b Version: $LATEST
module initialization error: ReferenceError
at get_json (/var/task/index.js:3:9)
at Object.<anonymous> (/var/task/index.js:20:18)
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)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
END RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b
REPORT RequestId: 516f164b-7ee3-11e7-97a7-0373caaf493b Duration: 80.81 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB
Any help would be greatly appreciated.

You have to install npm package for http in your project folder. Please follow below steps,
a. Open node command prompt
b. Navigate (CD ) to your project folder
c. Type command "npm init" if you don't have a package.json file already
e. Type command "npm install http --save" (Which will add http module to package.json)
f. At the beggining of you js file you using "http" please refer http moduleas below,
var http = require("http");
g. Add package.json file as well in the .zip to upload to Lambda (You don't need to include node modules folder)

You have to install the http module. npm install --save http
https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/

Related

Unexpected token in kafkajs code for a kafka consumer application

Nodejs version : 4.4.7
NPM version: 2.15.8
I am running the following code, which is literally copied from the following URL
https://kafka.js.org/docs/getting-started
const { Kafka } = require('kafkajs')
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['kafka1:9092', 'kafka2:9092']
})
The above code is saved in "apps.js" file under a "simpleapp" folder. In addition to that I have installed the following
npm install kafkajs
When I run the app.js file I get the following error
/Users/nick/Documents/nodeprojects/simpleapp/apps.js:1
(function (exports, require, module, __filename, __dirname) { const { Kafka } = require('kafkajs')
^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
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)
at startup (node.js:139:18)
at node.js:968:3
What is the issue?
Version 4.4.7 of NodeJS is very old, and isn't even supported anymore. KafkaJS follows the same support schedule as NodeJS itself, so the oldest currently supported version is 8.

Gcloud background functions with depedencies fail to deploy

Below is a basic example of gcloud background function with a dependency in it. On using uuid package it throws up an error although package.json has uuid
On deploying following erros is received.
gcloud beta functions deploy helloPubSub --trigger-resource my-topic --trigger-event google.pubsub.topic.publish
ERROR: (gcloud.beta.functions.deploy) OperationError: code=3, message=Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'uuid'
index.js
const uuid = require('uuid');
exports.helloPubSub = (event, callback) => {
const pubsubMessage = event.data;
const eventName = pubsubMessage.data ? Buffer.from(pubsubMessage.data, 'base64').toString() : 'World';
console.log(eventName,uuid.v4());
callback();
};
package.json
"dependencies": {
"uuid": "^3.2.1"
},
There was an issue in my .gcloudignore
Below change did make it work!
From
node_modules/
to
node_modules

AWS Lambda Parse JSON (Unexpected token)

I am trying to get a Lambda function to access an API and return the JSON.
API
http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a
P.S This API ID is the demo one provided by OW.
Lambda Code
var jsonurl = "http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a";
var data = JSON.parse(jsonurl);
exports.handler = function(event, context) {
console.log(data);
context.done(null, data); // SUCCESS with message
};
Error
{
"errorMessage": "Unexpected token h",
"errorType": "SyntaxError",
"stackTrace": [
"Object.parse (native)",
"Object.<anonymous> (/var/task/index.js:3:17)",
"Module._compile (module.js:456:26)",
"Object.Module._extensions..js (module.js:474:10)",
"Module.load (module.js:356:32)",
"Function.Module._load (module.js:312:12)",
"Module.require (module.js:364:17)",
"require (module.js:380:17)"
]
}
Log output
START RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45 Version: $LATEST
Syntax error in module 'index': SyntaxError
at Object.parse (native)
at Object.<anonymous> (/var/task/index.js:3:17)
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)
END RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45
REPORT RequestId: 8ca0fbd1-eee5-11e5-b9dd-31048a8d5a45 Duration: 173.76 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 9 MB
Can anyone see the problem?
What I would like is for lambda to get the json and return it, so anyone looking at my API url will see the results from the Open Weather API
You are passing a URL to JSON.parse(), not a JSON string. First you need to go get the JSON data from the URL using something like http.get(). Perhaps check out the answers to this similar question: Parsing JSON data from a URL

grunt Cannot read property 'name' of undefined

i try grunt on my windows machine. if i use yo webapp all work fine, but if i try to use grunt on exist project i have some problem.
my Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
banner:"<%= pkg.name %>"
});
//Load the plugin that provides the "uglify" task.
//grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.log.writeln("____________ log __________________");
grunt.log.writeln(grunt.pkg.name);
grunt.log.writeln(grunt.pkg.banner);
grunt.log.writeln();
grunt.log.writeln();
// Default task(s).
grunt.registerTask('default');}
package.json
{
"name": "bsworld",
"version": "0.1.0",
"description": "mio progetto personale",
"devDependencies": {
"grunt": "^0.4.1"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "grunt test"
}
}
this is the output of grunt --verbose
Initializing
Command-line options: --verbose
Reading "Gruntfile.js" Gruntfile...OK
Registering Gruntfile tasks.
Initializing config...OK
______ log ____________
Loading "Gruntfile.js" tasks...ERROR
>> TypeError: Cannot read property 'name' of undefined
>> at Object.module.exports (C:\Apache\htdocs\bottonisworld.com\Gruntfile.js:13:29)
at loadTask (C:\Apache\htdocs\bottonisworld.com\node_modules\grunt\lib\grunt \task.js:325:10)
at Task.task.init (C:\Apache\htdocs\bottonisworld.com\node_modules\grunt\lib\grunt\task.js:437:5)
at Object.grunt.tasks (C:\Apache\htdocs\bottonisworld.com\node_modules\grunt\lib\grunt.js:120:8)
at Object.module.exports [as cli] (C:\Apache\htdocs\bottonisworld.com\node_modules\grunt\lib\grunt\cli.js:38:9)
at Object.<anonymous> (C:\Users\l.bottoni\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt:45:20)
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)
No tasks specified, running default tasks.
Running tasks: default
Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.
why grunt don' load the file json?????
resolved, the json is loaded but i must get data with the correct method
grunt.log.writeln(grunt.config("pkg.name"));

Can't install Node.js plugin on Ubuntu 12.04

I have installed this database plugin for Node.js called 'db-mysql' and I've gone through all the steps mentioned on the site without getting errors, which must mean that the plugin is installed. But when I run my .js file (given below) in the Apache server I get this error.
Error: Cannot find module 'db-mysql'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/var/www/server.js:4:13)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
Here is the sample code I'm running.
var http = require("http");
var mysql = require("db-mysql");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
//Database Connectivity//
new mysql.Database({
hostname: 'localhost',
user: 'root',
password: '',
database: 'test'
}).on('error', function(error) {
console.log('ERROR: ' + error);
}).on('ready', function(server) {
console.log('Connected to ' + server.hostname + ' (' + server.version + ')');
}).connect();
///////////////////////
response.end();
}).listen(8889);
Did I miss anything during installation?
If you install module like in example
npm install db-mysql
Node will search it here
./project_root/node_modules/db-mysql
So make sure this path is correct in your case.
Other option is to install it globally by using -g flag
npm install db-mysql -g
Simply make a sym link that points to where your modules are physically installed
Name this link node_modules and put it inside the folder of your project