Read google calendar events - node.js

I want to read the events of a public google calendar without Oauth authentication.
But i can't get a response:
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({
extended: true
}));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req,res){
request("https://www.googleapis.com/calendar/v3/calendars/XXXXXXXXXXXX.calendar.google.com/events",function(response){
var calendarSummary = response.summary;
var calendarDescription =response.description;
console.log("Response is:");
console.log("Result is: "+calendarSummary+calendarDescription);
});
});
app.listen(3000, function() {
console.log("server is running on port 3000");
});
Any suggestions? Do i need to include an API-key for a public calendar?

I think the issue is your usage of the request library/function. Per the docs The signature for that function is request(error, response, body), but you only supply one argument. Despite calling it response, it's actually the error object which is probably null. So, change it to the following and I bet it'll work:
request("https://www.googleapis.com/calendar/v3/calendars/XXXXXXXXXXXX.calendar.google.com/events", function(error, response, body) {
var calendarSummary = body.summary;
var calendarDescription = body.description;
console.log("Response is:");
console.log("Result is: " + calendarSummary + calendarDescription);
});
I think your API call to Google is fine as-is because the documentation says no authorization is needed.

Related

How to execute commands in backend(Node.js Express) synchronously step by step?

I want to do this. But I am not able to run all of this synchronously. Every step is executing all at once. Can anyone help?
I need to do this backend task in Node.js express.
Step 1: Enter the keyword from the front end(written in react)
Step 2: Run the command: " shell.exec(__dirname + /run.sh "${data}"); " at the backend. [[data is the keyword received from the front end]]. It will search the keyword in my file and generate a csv file.
Step 3: Run a python script which will execute a python file(csv_to_html.py) after completion of Step 2 execution. It will convert the csv file generated to html table and create a output.html file
Step 4: On completion of step 3, an output.html file will be generated. Send this file in an iframe on the front screen(index.html or index.ejs or index.hbs) below the search box. Suggestions are welcome on how can I display it on the frontend.
All this should be done dynamically.
This is what I have done:
const { response } = require("express");
const express = require("express");
const bodyParser = require("body-parser");
const shell = require("shelljs");
const app = express();
const spawn = require("child_process").spawn;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
var data = "";
app.get("/", function (req, res) {
shell.exec(__dirname + `/run.sh "${data}"`);
const pythonProcess = spawn('python', ["csv_to_html.py"]);
res.sendFile(__dirname + "/index.html");
});
app.post("/", function (req, res) {
data = req.body.load_data;
res.redirect("/"));
});
app.listen(3000, function () {
console.log("Starting server at port 3000...");
});
Enable json parsing of the body data.
Move execution of scripts to our POST request, so that they only run when new data is sent to the server.
Remove shelljs and rely only on Node.JS' own child_process to invoke commands.
Nest our callbacks so that they execute in the desired order.
Following these steps, you'd end up with the following file (comments with numbers added in the lines modified):
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
// Modification #3
const { exec } = require("child_process");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json()); // Modification #1
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", async function (req, res) {
// Modification #2
const data = req.body.load_data;
// Modification #3 & #4
exec(`./run.sh "${data}"`, async (err, stdout, stderr) => {
if (err) console.error(err)
console.log(stdout);
exec('python csv_to_html.py', (err, stdout, stderr) => {
console.log(stdout);
res.redirect("/");
});
});
});
app.listen(3000, function () {
console.log("Starting server at port 3000...");
});
The body of the request sent to the POST route should be of Content-Type application/json, and similar to the following:
{
"load_data": "argumentForRunSH"
}

Ajax - POST request content doesn't get to the server

The problem I'm having is, the content that I try to send in my post request to the server doesn't get sent, but the request works.
Here's the code for the client:
$("#searchBtn").click(function(e){
try{
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/search/searchRequest", true);
console.log(($("#searchedSymptoms").val())) // gets posted in the console correctly
xhttp.setRequestHeader("Content-type", "text/plain"); // doesn't work without it either
xhttp.send($("#searchedSymptoms").val());
//xhttp.send(JSON.stringify($("#searchedSymptoms").val())); // doesn't work either
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
console.log(xhttp.responseText); // gets the correct response from server
}
else{
console.log(xhttp.responseText);
}
};
}
catch(err){
console.log(err);
}
});
And here's the server-side code:
var express = require("express");
var router = express.Router();
router.post("/searchRequest", function(req, res, next){
console.log("must get the client request:");
console.log(req.body);
//console.log(JSON.stringify(req.body)); // doesn't work either
});
In the server, what get's outputed to the console is this:
{}
Any thoughts on what I'm doing wrong ?
You need to use a text body-parser, Express won't do it by default, here's an example, using pretty much the same server side code you are:
"use strict";
var express = require("express");
var router = express.Router();
var bodyParser = require("body-parser");
router.post("/searchRequest", function(req, res, next){
console.log("must get the client request:");
console.log("SearchRequest: " + req.body);
res.end('ok', 200);
});
var port = 8081;
var app = express();
app.use(bodyParser.text());
app.use(router);
app.listen(port);
console.log("Express listening on port " + port);
You can configure the way the text body parser operates exactly by using the guide here:
https://www.npmjs.com/package/body-parser#bodyparsertextoptions

How to fetch data in mongoDB to show in localhost (Node.js)

I am working on to do list app using mongoDB and node.js. Basically you type what you want to do then click add. I successfully connected the database but it doesn't show the text that's in the database. It shows only the bullets in the localhost.
Here's the code:
app.get('/', function(req, res) {
db.collection('list').find().toArray(function (err, result) {
console.log(result);
if (err) {return};
console.log(err);
res.render('index.ejs', {list: result})
});
});
app.post('/', function(req, res){
console.log(req.body);
db.collection('list').save(req.body, function(err, result) {
if (err) {return};
console.log(err);
console.log('saved')
res.redirect('/');
})
})
I have validated the code you posted and have revised it slightly with comments.
I hope this helps but it seems that the fault might be in the res.render method that is being used. Please refer to the following code:
// Requires
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
// Instantiation
var app = express();
var mongopath = "mongodb://localhost:27017/BitX";
// Port number the REST api works on
var portnum = 7500;
// MongoDB object
var db = null;
MongoClient.connect(mongopath, function(err,ldb){
db = ldb;
});
// Implement Body Parser
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
// Start the REST service
var server = app.listen(portnum, function() {
var host = server.address().address;
var port = server.address().port;
console.log("Content Provider Service listening at http://%s:%s", host, port);
});
// Default route
app.get('/', function(req, res) {
// Find all items in orders and send back results to a front end
db.collection('orders').find().toArray(function (err, result) {
res.send(result);
// Consider that the rendering engine may not be functioning correctly
// SEE MORE: https://stackoverflow.com/questions/21843840/what-does-res-render-do-and-what-does-the-html-file-look-like
//res.render('index.ejs', {list: result})
});
});
// Accept a post on the root
app.post('/', function(req, res){
//Save into orders
db.collection('orders').save(req.body, function(err, result) {
res.send(true);
//res.redirect('/');
});
});
For additional information on the res.render method please have a look at:
What does "res.render" do, and what does the html file look like?
- if you have not already.
Hope it helps!

NodeJS/Express GET exit status of external application

I am pretty new to NodeJS and this is my first time with Express 4, I'm trying to build a simple RESTful front-end around a command-line application. There will ultimately only be one GET and one POST necessary, with the POST handling about 3 or 4 different parameters. The GET should call the command-line application with all default parameters, which is basically just a status check and return the exit status upon completion. The POST will pass along POST parameters on the commandline. I know that this basically calls for an asynchronous call, like child_process.execFile(), but I can't seem to figure out how to actually return the response from within the callback function.
This is the tutorial I used as a starting point, omitting the mongoose dependency, because I have no need for MongoDB, so I basically just followed it up to the point where you start the server. At this point, I'm pretty lost. I always hate writing async code...
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var child_process = require('child_process');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
var router = express.Router(); // get an instance of the express Router
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
router.get('/myapp/status', function(req, res) {
console.log(req.user);
child_process.execFile(
'casperjs',
['myfile.js', '--cmd="Status"', '--user="myuser"', '--pass="#mypass"'],
null,
function(response) {
// ???
}, res);
});
app.use('/api', router);
app.listen(port);
console.log('Magic happens on port ' + port);
You can try the following:
router.get('/myapp/status', function(req, res) {
console.log(req.user);
child_process.execFile(
'casperjs', //command
["myfile.js --cmd=Status --user=myuser --pass=#mypass"], // args
function(err, stdout, stderr) { //callback
if (err) {
return res.status(500).send(err);
}
res.send(stdout); // to send response to client
});
});

How to capture (or log) the data of the Response object in Express?

I need to capture de sended data. I know how to set a middleware and capture the request and the response header.
I'm asking for capture the sent data.
I'm trying this:
var express = require('express')
var app = express();
var onFinished = require('on-finished')
app.listen(3000);
function logRes(res){
console.log('* captured response *')
console.log(res._header)
console.log(res.statusCode)
console.log(res.statusMessage)
console.log(res.body) // THIS DOES NOT WORKS!
}
app.use(function(req,res,next){
onFinished(res, function (err, res) {
logRes(res)
})
console.log('= captured request =');
console.log(req.method)
console.log(req.path)
console.log(req.query)
next();
});
app.get('/example',function(req,res){
res.header('Content-Type','text/plain');
res.end('example data');
});
Can any says to me how to look at the sent data in the line with the // THIS DOES NOT WORKS! comment?
You first need to include the third party middleware body-parser:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.listen(3000);
app.use(bodyParser.json({ type: 'application/*+json' }));
app.use(function(req,res,next) {
console.log('= captured request =');
console.log(req.method);
console.log(req.path);
console.log(req.query);
console.log(req.body);
next();
});
app.get('/example',function(req,res) {
res.header('Content-Type','text/plain');
res.end('example data');
});
Check the repo and documentation here.

Resources