1.0.6 • Published 4 years ago

file-function-server v1.0.6

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

npm version

🕊File Function Server

No-Frills Node.js API Endpoints

⏰ 60-Second Setup Starting... Now!

Requirements: Node.js >=7, NPM or Yarn

⬇️ Install

npm init (if fresh project)

npm i file-function-server or yarn add file-function-server

📂 Create Functions Folder

📦your-project
 ┣ 📂functions
 ┗ 📜package.json

👨‍🏭 Create File Function

Routes are defined by naming files & folders

📦your-project
 ┣ 📂functions
 ┃ ┗ 📜hello-world.js
 ┗ 📜package.json

The endpoint handler is the default node module export. It takes two arguments: 1. req (express.Request type) 2. res (express.Response type)

hello-world.js (JavaScript)

module.exports.default = function (req, res) {
  return 'Hello World!';
};
export default (req, res) => 'Hello World!';
import { FileFunctionHandler } from 'file-function-server';

export default ((req, res) => {
 // Intellisense enabled!
 return 'Hello World!';
}) as FileFunctionHandler;

Script

Finally, add the following to your package.json

"scripts": {
 "start": "file-function-server"
}

📻 Generated API

Run npm run start or yarn start from the command line.

Your API Endpoints are visible at localhost:9000/functions

EndpointGETPOSTPUTPATCHDELETE
/functions/hello-world

Programmatic Usage

const { FileFunctionServer } = require('file-function-server');
new FileFunctionServer(/* optional config */).start();

Custom Function Directory

const { FileFunctionServer } = require('file-function-server');
const path = require('path');
new FileFunctionServer({
 functionsDir: path.join(__dirname, '/api-functions')
}).start();

⚙️ Config Object

PropertyDescriptionRequiredDefault
appExpress App🚫New express app
functionsDirDirectory containing file functions🚫<current-working-directory>/functions
portPort to start server on🚫PORT Environment Variable or 9000