Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mccallofthewild/file-function-server

๐Ÿ™…โ€โ™€๏ธ No-Frills Node.js API Endpoints
https://github.com/mccallofthewild/file-function-server

Last synced: 4 days ago
JSON representation

๐Ÿ™…โ€โ™€๏ธ No-Frills Node.js API Endpoints

Awesome Lists containing this project

README

        

[![npm version](https://badge.fury.io/js/file-function-server.svg)](https://badge.fury.io/js/file-function-server)

# ๐Ÿ•Š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)
```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`
```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

```javascript
const { FileFunctionServer } = require('file-function-server');
new FileFunctionServer(/* optional config */).start();
```
Custom Function Directory
```javascript
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 | ๐Ÿšซ | `/functions` |
| port | Port to start server on | ๐Ÿšซ | `PORT` Environment Variable or `9000` |