JavaScript

JavaScript

The Endpoint Function

Napkin includes its own Node.js 14.x runtime with additional features for handling HTTP traffic. To get access to incoming request data and outgoing response data, simply export a default function with 2 parameters like this:

export default (req, res) => {
  // any logic here
}

The function should have 2 parameters: req and res . For instance, you can access the incoming request parameters via the req.params function and send back content in the response via res.send. These objects are very similar to the Express' request and response APIs: https://expressjs.com/en/api.html#req

Incoming Request Data

req.body

type: Object

Contains key-value pairs of data submitted in the request body if the incoming Content-Type was application/json

req.cookies

type: Object

Contents of all cookies transmitted in the request

req.data

type: String

Contains the incoming request data as a string in case it came it came with a non-application/json Content-Type header.

req.form

type: Object

Contains key-value pairs of incoming form data if the incoming Content-Type request header is multipart/form-data

req.get(field)

type: Function

Return the value for the the HTTP request header field field (case-insensitive match).

req.headers

type: Object

Contains the incoming request headers as key-value pairs

req.method

type: String

The HTTP method of the incoming request

req.param

type: Function

Returns the value for the given path parameter, or undefined if no such parameter exists in the request

req.params

type: Object

Contains key-value pairs of the path parameters of the incoming request

req.path

type: String

The path part of the request URL

req.query

type: Object

Contains key-value pairs for any query parameters present in the incoming request route

Response Object

res.append

type: Function

Appends the specified value to the HTTP response header field. If the header is not already set, it creates the header with the specified value. The value parameter can be a string or an array

res.headers

type: Object

Object for storing key-value pairs of response headers

res.json

type: Function

Stores the given Object or Array as JSON payload in the response body.

res.send

type: Function

Sends the given String back in the HTTP response body

res.set(field, [, value])

type: Function

Sets the response's HTTP header field to value

res.status(statusCode)

type: Function

Sets the response's status code to statusCode

Installing Modules

Once you've created an account, the Napkin editor allows you to install any NPM module or package.

Currently, all installed NPM modules are shared across a user's functions. Just install a module once and it will be there ready to use in any of your other functions. Currently, combined module storage space is limited to 250MB.

Napkin supports both CommonJS and ES6 module syntax, so both of the following work:

import _ from 'lodash'
// or
const _ = require('lodash')

Environment Variables

As a signed in user, you can add environment variables to your user functions:

  1. Go to Other tab > Environment Variables
  2. Add key and value

Using Environment Variables

Then, to use environment variables in Node.js, use the built-in process.env global property. For example, you can print all environment variables in your function:

console.log(process.env)

Files

You can upload files to use in your Napkin functions. Go to the Other tab in the Napkin editor and scroll down to Files. You can upload any file as long as its size is less than 1MB.

Once uploaded, your function can access your files via the /opt/files directory. You can access your files using the usual builtin modules for both Javascript and Python. For example, assuming you've uploaded a file called file.txt, here's how you can print the contents of the file:

import fs from 'fs'

const data = fs.readFileSync('/opt/files/file.txt', 'utf-8')
console.info(data)

Key Value Store

The store module lets allows you to persist any valid JSON data type across your function runs:

  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • null
import { store } from 'napkin'

async store.get(key)

Returns: Object ({ data: ... })

Retrieve the value associated with the key key . The value is return in a dictionary with a single key, data

await store.put('myKey', 42)
const { data } = await store.get('myKey')
console.log(data)
// 42

async store.has(key)

Returns: Boolean

Return whether or not the key-value store contains a key-value pair with key key

await store.put('myKey', 42)
let exists = await store.has('myKey'))
// true

await store.delete('myKey')
exists = await store.has('myKey')
console.log(exists)
// false

async store.put(key, value)

Returns: null

Save the given key-value pair in the Store. If the key-value pair already exists in the store, the value is updated accordingly.

await store.put('myKey', 42)
let exists = await store.has('myKey'))
// true

async store.size()

Returns: Number

Return the current size in bytes of the key-value store. Maximum size is 400 KB.

const size = await store.size()

console.log(size)
// 128

async store.delete(key)

Delete the value stored under the key key

await store.put('myKey', 42)
let exists = await store.has('myKey')
console.log(exists)
// true

await store.delete('myKey')
exists = await store.has('myKey'))
console.log(exists)
// false

const { data } = await store.get('myKey')
console.log(data)
// null