Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.
/ server_v1 Public archive

⛔️ DEPRECATED, Core module for building the Sheetbase backend app.

License

Notifications You must be signed in to change notification settings

sheetbase/server_v1

Repository files navigation

⛔️ DEPRECATED

We are updating version 3 of the module: https://github.com/sheetbase/server!

Sheetbase Module: @sheetbase/server

Sheetbase core module for backend app.

Build Status Coverage Status NPM License clasp Support me on Patreon PayPal Ask me anything

Install

Using npm: npm install --save @sheetbase/server

import * as Sheetbase from "@sheetbase/server";

As a library: 1bhE_YkXnzZLr9hZk_5NXgCY6bAe73UHIGjM4dvyRJLLTyccpu5vS6jeJ

Set the Indentifier to SheetbaseModule and select the lastest version, view code.

declare const SheetbaseModule: { Sheetbase: any };
const Sheetbase = SheetbaseModule.Sheetbase;

Scopes

https://www.googleapis.com/auth/script.scriptapp

Usage

Getting started

Install: npm install --save @sheetbase/server

Usage:

import { sheetbase } from "@sheetbase/server";

const Sheetbase = sheetbase({
  /* configs */
});

Sheetbase.Router.get("/", (req, res) => {
  return res.send("Hello!");
});

Configs

Sheetbase app configurations.

allowMethodsWhenDoGet

  • Type: boolean
  • Default: false

Allows POST, PUT, ... when do GET method, useful for testing in browser without re-deploy the web app.

views

  • Type: string
  • Default: ''

Views folder, where to put view templating file. Examples: views, public/views

disabledRoutes

  • Type: string[]
  • Default: []

List of disabled routes, format: method:endpoint. Examples: [ 'GET:/', 'POST:/me' ]

routingErrors

List of routing errors, by codes, when do res.error(code), the router will show the coresponding error to the client.

Examples: { foo: 'The Foo error.' }. Then: res.error('foo'), the result will be:

{
  "error": true,
  "status": 400,
  "code": "foo",
  "message": "The Foo error."
}

Routing

Sheetbase provide a routing system like ExpressJS.

The router supports these methods: GET, POST, PUT, PATCH, DELETE. But only GET and POST is real HTTP methods, others are just play as a way to organize the app.

router.get('/', ...), accepts a GET request to ...?e=/

router.post('/', ...), accepts a POST request to ...?e=/

router.put('/', ...), accepts a POST request to ...?e=/&method=PUT

router.patch('/', ...), accepts a POST request to ...?e=/&method=PATCH

router.delete('/', ...), accepts a POST request to ...?e=/&method=DELETE

router.all('/', ...), accepts all request to ...?e=/

Basic use

router.get("/", (req, res) => {
  return res.send("Hello, world!");
});

Request

{
  query: Object;
  params: Object; // same as query
  body: Object;
}

Response

  • send: return string as html or object as json.
  • html: return html page.
  • json: return json data.
  • render: render html template, supports native GAS template, Handlebars and Ejs.
  • success: return json data in form of a ResponseSuccess.
  • error: return json data in form of a ResponseError.

Routing errors

Use setErrors to resgister your app routing errors. So that you can just do res.error(code) later.

// set errors
router.setErrors({
  error1: "Error 1",
  error2: { status: 500, message: "Error 2" }
});

// later, in a route
return res.error("error1");

With middlewares

For a certain route.

const Middleware = (req, res, next) => {
  return next();
};

router.get("/", Middleware, (req, res) => {
  return res.send("Final!");
});

For all routes.

router.use((req, res, next) => {
  return next();
});

Middleware data

Want to send data downstream.

return next({ foo: "bar" });

Render HTML template

To render HTML template with Handlebars or Ejs.

Place your template files with the coresponding extensions somewhere in the project, may be views folder. Remmber to set it in configs.

For Handlebars, file extension would be .hbs.html, for Ejs it is ejs.html;

Add the vendor js to your app, before main app code. With app-scripts, the build script would be sheetbase-app-scripts build --vendor <path to js file>

License

@sheetbase/server is released under the MIT license.

About

⛔️ DEPRECATED, Core module for building the Sheetbase backend app.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published