No-Frills Node.js API Endpoints
Requirements: Node.js >=7
, NPM
or Yarn
npm init
(if fresh project)
npm i file-function-server
or
yarn add file-function-server
📦your-project
┣ 📂functions
┗ 📜package.json
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:
req
(express.Request
type) res
(express.Response
type)hello-world.js (JavaScript)
module.exports.default = function (req, res) {
return 'Hello World!';
};
```javascript export default (req, res) => 'Hello World!'; ```
```typescript import { FileFunctionHandler } from 'file-function-server'; export default ((req, res) => { // Intellisense enabled! return 'Hello World!'; }) as FileFunctionHandler; ```
Finally, add the following to your package.json
"scripts": {
"start": "file-function-server"
}
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 |
✅ | ✅ | ✅ | ✅ | ✅ |
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();
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 |