Skip to content

Instantly share code, notes, and snippets.

View andijakl's full-sized avatar

Andreas Jakl andijakl

View GitHub Profile
@andijakl
andijakl / JavaScript-Try-Catch.js
Created September 12, 2018 12:37
Try / Catch in JavaScript
var weight = -12.2;
try {
if (weight < 0) {
throw "Weight can not be less than 0";
}
console.log("Saving today's weight to database...");
} catch(error) {
console.log(error);
}
@andijakl
andijakl / index.js
Created October 8, 2018 16:10
JavaScript Callback Example using Node.js and the request module
const request = require("request");
request("https://www.andreasjakl.com/", function(error, response, body) {
if (error) {
// Handle error
console.log("Error: ", error);
} else {
// Successful request
console.log("body: ", body);
}
@andijakl
andijakl / index.js
Created October 8, 2018 16:28
Node.js callback example 2 - separate function instead of anonymous functions
const request = require("request");
request("https://www.andreasjakl.com/", handleResponse);
function handleResponse(error, response, body) {
if (error) {
// Handle error
console.log("Error: ", error);
} else {
// Successful request
@andijakl
andijakl / callbackhell.js
Created October 8, 2018 16:31
Example for the "Callback Hell" with chained web requests
request("https://www.andreasjakl.com/wp-content/uploads/2018/08/arcore-anchors.gif", function(error, response, body) {
if (error) {
// Handle error
console.log("Error: ", error);
} else {
if (response && response.statusCode == 200) {
request("https://www.andreasjakl.com/wp-content/uploads/2018/08/SLAM-HoloLens-2000x1163.jpg", function(error, response, body) {
if (error) {
console.log("Error: ", error);
} else {
@andijakl
andijakl / promises.js
Created October 8, 2018 16:41
Node.js chained HTTP requests using promises through the request-promise-native module
const rp = require("request-promise-native");
rp("https://www.andreasjakl.com/")
.then(function(response) {
// Start second request, e.g., based on results of first request
console.log("Response length: ", response.length);
if (response.length > 100) {
return rp("https://www.andreasjakl.com/wp-content/uploads/2018/08/arcore-anchors.gif");
}
})
@andijakl
andijakl / cosmos-db-database-connect.js
Created October 10, 2018 17:50
Open a connection to the Cosmos DB database
try {
// Open a reference to the database
const dbResponse = await cosmosClient.databases.createIfNotExists({
id: databaseId
});
var database = dbResponse.database;
// ... (we will add more code here!)
res.send("Finished");
@andijakl
andijakl / add-item-cosmos-db.js
Created October 11, 2018 07:57
Add an item to the Cosmos DB from Node.js
const newItemId = Math.floor(Math.random() * 1000 + 10).toString();
let documentDefinition = {
"id": newItemId,
"name": "Angus MacGyver",
"state": "Building stuff"
};
// Add a new item to the container
console.log("** Create item **");
const createResponse = await container.items.create(documentDefinition);
@andijakl
andijakl / document-db-sql-query.js
Created October 11, 2018 08:30
Create a new item in a Document DB through an SQL query (warning: unsafe!)
const queryResponse = await container.items.query(
"SELECT * FROM c WHERE c.id='" + newItemId + "'").toArray();
console.log(queryResponse.result[0].name);
@andijakl
andijakl / cosmos-db-delete-readall.js
Created October 11, 2018 09:23
Delete an item and retrieve all items from an Azure Cosmos DB
// Delete item
const deleteResponse = await container.item(newItemId).delete();
console.log(deleteResponse.item.id);
// Read all items from the container
const docResponse = await container.items.readAll().toArray();
res.send(docResponse);
@andijakl
andijakl / index.js
Last active October 11, 2018 09:58
Start template for Express web server in Node.js
// Import the "express" module for more powerful web server capabilities.
const express = require('express');
// Initialize the express module and make it accessible via the app variable.
const app = express()
app.get('/', async (req, res) => {
// We'll write all our code here. For now, to test
res.end("Hello DB!");
});