mccallofthewild / file-function-server

🙅‍♀️ No-Frills Node.js API Endpoints
0 stars 0 forks source link

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!';
};
hello-world.js (ES6)

```javascript export default (req, res) => 'Hello World!'; ```

hello-world.ts (TypeScript)

```typescript 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

Endpoint GET POST PUT PATCH DELETE
/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

Property Description Required Default
app Express App 🚫 New express app
functionsDir Directory containing file functions 🚫 <current-working-directory>/functions
port Port to start server on 🚫 PORT Environment Variable or 9000