Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/mccallofthewild/file-function-server
- Owner: mccallofthewild
- Created: 2020-03-08T07:00:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-07-19T19:55:33.000Z (over 4 years ago)
- Last Synced: 2024-04-26T03:44:57.082Z (7 months ago)
- Language: TypeScript
- Homepage:
- Size: 336 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
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` |