We are updating version 3 of the module: https://github.com/sheetbase/server!
Sheetbase core module for backend app.
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;
https://www.googleapis.com/auth/script.scriptapp
-
Docs homepage: https://sheetbase.github.io/server
-
API reference: https://sheetbase.github.io/server/api
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!");
});
Sheetbase app configurations.
- Type:
boolean
- Default:
false
Allows POST, PUT, ... when do GET method, useful for testing in browser without re-deploy the web app.
- Type:
string
- Default:
''
Views folder, where to put view templating file. Examples: views
, public/views
- Type:
string[]
- Default:
[]
List of disabled routes, format: method:endpoint
. Examples: [ 'GET:/', 'POST:/me' ]
- Type: RoutingErrors
- Default:
{}
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."
}
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=/
router.get("/", (req, res) => {
return res.send("Hello, world!");
});
{
query: Object;
params: Object; // same as query
body: Object;
}
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.
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");
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();
});
Want to send data downstream.
return next({ foo: "bar" });
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>
@sheetbase/server is released under the MIT license.