Sheetbase core module for backend app.
Git repo: https://github.com/sheetbase/server/
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.
boolean
false
Allows POST, PUT, … when do GET method, useful for testing in browser without re-deploy the web app.
string
''
Views folder, where to put view templating file. Examples: views
, public/views
string[]
[]
List of disabled routes, format: method:endpoint
. Examples: [ 'GET:/', 'POST:/me' ]
{}
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>