Skip to content

Instantly share code, notes, and snippets.

module.exports = function (Frog) {
/**
* Helper functions can go here.
*/
Frog.uploadFrogImage = async (id, req) => {
...
};
Frog.remoteMethod('uploadFrogImage', {
Frog.uploadFrogImage = async (id, req) => {
// Get the frog instance
const frog = await Frog.findById(id);
if (!frog) throw new Error('Frog not found.');
// extract the file from the request object
const file = await getFileFromRequest(req);
// upload the file
const { join, extname } = require('path');
const { readFileSync } = require('fs');
const AWS = require('aws-sdk');
const s3 = new AWS.S3({ /* CONFIG DATA */ });
/**
* Helper method which takes the request object and returns a promise with the AWS S3 object details.
*/
const uploadFileToS3 = (file, options = {}) => {
const multiparty = require('multiparty');
/**
* Helper method which takes the request object and returns a promise with a file.
*/
const getFileFromRequest = (req) => new Promise((resolve, reject) => {
const form = new multiparty.Form();
form.parse(req, (err, fields, files) => {
if (err) reject(err);
const file = files['file'][0]; // get the file from the returned files object
@jackrobertscott
jackrobertscott / package.json
Last active February 8, 2018 03:56
ZEIT Now single page app deployment package
{
"name": "myapp-spa",
"version": "1.0.0",
"scripts": {
"start": "serve --single --cache=60000"
},
"dependencies": {
"serve": "latest"
}
}
@jackrobertscott
jackrobertscott / 404.html
Created February 8, 2018 04:06
ZEIT Now single page app 404 file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>404 Not Found | My App</title>
</head>
<body>
<script>
const queryString = require('query-string');
...
const params = queryString.parse(document.location.search);
const redirect = params.redirect; // this would be "abcdefg" if the query was "?redirect=abcdefg"
if (document.location.pathname === '/' && redirect) {
document.location.assign(`${document.location.origin}/${redirect}`);
}
import { google } from 'googleapis';
const googleConfig = {
clientId: '<GOOGLE_CLIENT_ID>', // e.g. asdfghjkljhgfdsghjk.apps.googleusercontent.com
clientSecret: '<GOOGLE_CLIENT_SECRET>', // e.g. _ASDFA%DFASDFASDFASD#FAD-
redirect: 'https://your-website.com/google-auth' // this must match your google api settings
};
/**
* Create the google auth object which gives us access to talk to google's apis.
/**
* This scope tells google what information we want to request.
*/
const defaultScope = [
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/userinfo.email',
];
/**
* Get a url which will open the google sign-in page and request access to the scope provided (such as calendar events).