Package Exports
- @sheetbase/server
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sheetbase/server) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Sheetbase Module: @sheetbase/server
Sheetbase core module for backend app.
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
Docs homepage: https://sheetbase.github.io/server
API reference: https://sheetbase.github.io/server/api
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
- 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."
}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.