Skip to content

Instantly share code, notes, and snippets.

View ernestofreyreg's full-sized avatar
🚢
Just ship it

Ernesto Freyre ernestofreyreg

🚢
Just ship it
View GitHub Profile
@ernestofreyreg
ernestofreyreg / main.js
Created November 25, 2014 14:27
FizzBuzz nodeJS version, for academic/learning purposes.
http = require("http");
url = require("url");
fizzBuzz = function(v) {
return (v%15===0) && "FizzBuzz" || (v%3===0) && "Fizz" || (v%5===0) && "Buzz" || v;
};
http.createServer(function(req, res) {
var i = parseInt(url.parse(req.url).pathname.substring(1));
res.writeHead(200, {"Content-Type": "application/json"});
@ernestofreyreg
ernestofreyreg / index.js
Created May 15, 2017 02:05
First Google Cloud Function
module.exports.handler = function (req, res) {
return res.send('Hello World!')
}
@ernestofreyreg
ernestofreyreg / index.js
Created May 15, 2017 22:18
Simple backend to read contacts from a mongodb DB
require('dotenv').config()
import { MongoClient } from 'mongodb'
const connectMongoDB = () => MongoClient.connect(process.env.MONGODB)
const getContacts = (req, res) => {
return connectMongoDB()
.then(
db => db.collection('contacts')
.find({})
@ernestofreyreg
ernestofreyreg / index.js
Created May 15, 2017 23:10
Create and Read contacts
require('dotenv').config()
import { MongoClient } from 'mongodb'
const connectMongoDB = () => MongoClient.connect(process.env.MONGODB)
const getContacts = (req, res) => {
return connectMongoDB()
.then(
db => db.collection('contacts')
.find({})
@ernestofreyreg
ernestofreyreg / index.js
Created May 17, 2017 06:58
MongoDB read and create backend api
require('dotenv').config()
const { MongoClient } = require('mongodb')
const connectMongoDB = () => MongoClient.connect(process.env.MONGODB)
const getContacts = (req, res) => {
return connectMongoDB()
.then(
db => db.collection('contacts')
.find({})
@ernestofreyreg
ernestofreyreg / index.js
Last active May 17, 2017 20:12
Async microrouter version
require('dotenv').config()
import { MongoClient } from 'mongodb'
import { get, post, router } from 'microrouter'
const connectMongoDB = () => MongoClient.connect(process.env.MONGODB)
const getContacts = async (req, res) => {
const db = await connectMongoDB()
const contacts = await db.collection('contacts').find({}).toArray()
db.close()
module.exports.handler = (event, context, callback) => {
const response = {
statusCode: 200,
body: 'Hello World!',
}
callback(null, response)
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event })
@ernestofreyreg
ernestofreyreg / index-aws.js
Last active May 19, 2017 20:30
AWS support for GCF
const express = require('express')
const serverless = require('serverless-http')
const { handler } = require('./index')
const app = express()
app.use(handler)
module.exports.handler = serverless(app)
@ernestofreyreg
ernestofreyreg / serverless.yml
Created May 19, 2017 20:31
Serverless config for deploy GCF functions to AWS
service: hello-world
provider:
name: aws
runtime: nodejs6.10
functions:
hello:
handler: index-aws.handler
@ernestofreyreg
ernestofreyreg / index.js
Created May 23, 2017 23:04
Serverless react starting point
module.exports.handler = function (req, res) {
return res.send('My Serverless App')
}