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 / async-await.js
Created October 8, 2018 16:53
HTTPS requests using Node.js and async / await through the request-promise-native module
const rp = require("request-promise-native");
async function processData() {
try {
let response = await rp("https://www.andreasjakl.com/");
if (response.length > 100) {
let response2 = await rp("https://www.andreasjakl.com/wp-content/uploads/2018/08/arcore-anchors.gif");
console.log("Data: ", response2.substring(0,10));
}
} catch (error) {
@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!");
});
@andijakl
andijakl / cosmos-db-init.js
Last active December 22, 2021 09:32
Configure access to the Azure Cosmos DB
app.get('/', async (req, res) => {
// Import database node module
const CosmosClientInterface = require("@azure/cosmos").CosmosClient;
// Database and container IDs
const databaseId = "ToDoList";
const containerId = "Items";
// Configure database access URI and primary key
const endpoint = "https://<...>.documents.azure.com:443/";
@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);