Skip to content

Instantly share code, notes, and snippets.

@neverstew
Created March 24, 2019 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neverstew/1093c2d7618e4c2e895839a200a816e9 to your computer and use it in GitHub Desktop.
Save neverstew/1093c2d7618e4c2e895839a200a816e9 to your computer and use it in GitHub Desktop.
Serverless Feather Application
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const serverless = require('serverless-http');
class Messages {
constructor() {
this.messages = [];
this.currentId = 0;
}
async find(params) {
// Return the list of all messages
return this.messages;
}
async get(id, params) {
// Find the message by id
const message = this.messages.find(message => message.id === parseInt(id, 10));
// Throw an error if it wasn't found
if(!message) {
throw new Error(`Message with id ${id} not found`);
}
// Otherwise return the message
return message;
}
async create(data, params) {
// Create a new object with the original data and an id
// taken from the incrementing `currentId` counter
const message = Object.assign({
id: ++this.currentId
}, data);
this.messages.push(message);
return message;
}
async patch(id, data, params) {
// Get the existing message. Will throw an error if not found
const message = await this.get(id);
// Merge the existing message with the new data
// and return the result
return Object.assign(message, data);
}
async remove(id, params) {
// Get the message by id (will throw an error if not found)
const message = await this.get(id);
// Find the index of the message in our message array
const index = this.messages.indexOf(message);
// Remove the found message from our array
this.messages.splice(index, 1);
// Return the removed message
return message;
}
}
// This creates an app that is both, an Express and Feathers app
const app = express(feathers());
// Turn on JSON body parsing for REST services
app.use(express.json());
// Turn on URL-encoded body parsing for REST services
app.use(express.urlencoded({ extended: true }));
// Set up REST transport using Express
app.configure(express.rest());
// Set up an error handler that gives us nicer errors
app.use(express.errorHandler());
// Initialize the messages service by creating
// a new instance of our class
app.use('messages', new Messages());
// Export the wrapped application as the 'handler' method
module.exports.handler = serverless(app);
{
"name": "feather-serverless",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ."
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@feathersjs/express": "^1.3.1",
"@feathersjs/feathers": "^3.3.1",
"serverless-http": "^1.9.1"
}
}
service: feather-serverless
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-2
functions:
app:
handler: index.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment