Skip to content

Instantly share code, notes, and snippets.

@jbergant
jbergant / denofetch.ts
Created July 22, 2020 10:54
Deno fetch examples
// Fetch Watson Tone Analyzer. Use basic key authentocation
// Get key and url here: https://www.ibm.com/watson/services/tone-analyzer/
const sentimentAnalysis = async (text: string): Promise<any> => {
const env = Deno.env.toObject();
const SENTIMENT_KEY = env.SENTIMENT_KEY || "sentish";
const SENTIMENT_URI = env.SENTIMENT_URI || "localhost";
const response = await fetch(
`${SENTIMENT_URI}&text=${encodeURI(text)}`,
{
@jbergant
jbergant / mongoexamples.ts
Created July 1, 2020 14:13
Deno with MongoDb examples
import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
interface Course {
_id: { $oid: string };
title: string;
author: string;
rating: number;
}
const client = new MongoClient();
@jbergant
jbergant / controllers-courses.ts
Created July 1, 2020 10:25
Deno - simple API
import { Course } from "../models/course.ts";
import { courses } from "../data/data.ts";
// deno-lint-ignore no-explicit-any
export const getCourses = ({ response }: { response: any }) => {
response.body = courses;
};
export const getCourse = ({
params,
@jbergant
jbergant / test.ts
Created June 16, 2020 12:11
deno test
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
Deno.test("deno test", () => {
const name = "Jana";
const surname = "Bergant";
const fullname = `${name} ${surname}`;
assertEquals(fullname, "Jana Bergant");
});
@jbergant
jbergant / index.ts
Created June 10, 2020 10:41
Deno AOK simple demo
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const app = new Application();
const router = new Router();
const PORT = 8000;
const HOST = "localhost";
export const getHome = ({ response }: { response: any }) => {
response.body = "Home page";
};
@jbergant
jbergant / app.ts
Created June 9, 2020 07:47
Deno simple API for IT courses
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const env = Deno.env.toObject();
const PORT = Number(env.PORT) || 8000;
const HOST = env.HOST || "localhost";
interface Course {
title: string;
author: string;
rating: number;
@jbergant
jbergant / index.ts
Created June 5, 2020 18:19
deno server with env variables
import { serve } from "https://deno.land/std/http/server.ts";
const env = Deno.env.toObject();
const PORT = Number(env.PORT) || 8000;
const HOST = env.HOST || "localhost";
const server = serve({ hostname: HOST, port: PORT });
console.log(`http://${HOST}:${PORT}`);
for await (const req of server) {
@jbergant
jbergant / index.ts
Created June 2, 2020 12:39
deno server
import { serve } from "https://deno.land/std/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
req.respond({ body: "Hello World\n" });
}