loopsailor v1.1.9
LoopSailor.js
LoopSailor makes it easy to build rest API's.
Want to see for yourself?
An example of usage in sample app repo, here.
Table of Contents
- Concepts - Environment - Bootstrap - Cors - Middleware - Logging - Policies - Routes
- App Structure - app.js - api/controllers - api/policies.js - config/env - config/bootstrap.js - config/http.js - config/index.js - config/log.js - config/policies.js - config/routes.js
Concepts
Environment
- Any files saved under the
/config/env/<environment-name>.env
folder will be loaded only when LoopSailor is served in theNODE_ENV
=<environment-name>
. For example, files saved underconfig/env/production.env
will only be loaded when LoopSailor is served withNODE_ENV=production
.
By default, your app runs in the development
environment. The recommended approach for changing your app's environment is by using the NODE_ENV
environment variable.
Bootstrap
This is a server-side file that is executed by LoopSailor before your app is served.
This gives you an opportunity to run jobs, or perform some special logic.
Cors
CORS is a mechanism that allows browser scripts on pages served from other domains to talk to your server. Like JSONP, the goal of CORS is to circumvent the same-origin policy.
LoopSailor use default express CORS, and you can configure it following the package usage.
HTTP
LoopSailor http can be configured as well as you need. By default LoopSailor has implemented 404 Handler
and Error Handler
.
HTTP Key | Purpose |
---|---|
cors | CORS is like a more modern version of JSONP-- it allows your application to circumvent browsers' same-origin policy. By default is set to true . |
handler404 | This is an 404 handler for use in both developmend and production environments. By default is set to true . |
handlerError | This is an error handler for use in both development (debug) and production environments.In production mode, loopsailor-error-handler omits details from error responses to prevent leaking sensitive information. By default is set to true . |
Middleware
LoopSailor is fully compatible with Express / Connect middleware, which are functions that accept req
, res
and next
as arguments. Every app utilizes a configurable middleware stack, just for handling HTTP requests. Each time the app receives an HTTP request, its configured HTTP middleware stack runs in order.
Built-in HTTP middlewares
HTTP Middleware Key | Purpose |
---|---|
bodyParser | Parses parameters and binary upstreams (for streaming file uploads) from the HTTP request body. |
compression | Compresses response data using gzip/deflate. See compression for details. |
cookieParser | Parses the cookie header into a clean object for use in subsequent middleware and your application code. |
expressHystrix | Hystrix component proved to be really useful in the service client pipeline to follow fail fast pattern as well as provide real-time metrics. |
helmet | Helmet helps you secure your Express apps by setting various HTTP headers. |
XGlobalTransactionId | Attaches an X-Global-Transaction-Id header to outgoing responses. |
Logging
LoopSailor comes with a built-in logger called log4js
. Its usage is functionally very similar to Node's console.log
, but with a handful of extra features, namely support for multiple log levels with colorized, prefixed console output. The logger serves one purposes:
- it emits warnings, errors, and other console output from inside the Loopsailor framework.
Log levels
Priority | Level | Log fns that produce visible output |
---|---|---|
0 | off | N/A |
1 | error | .error() |
2 | warn | .warn() .error() |
3 | info | .info() .warn() .error() |
4 | debug | .debug() .info() .warn() .error() |
Policies
Policies in LoopSailor are versatile tools for authorization and access control: they let you execute some logic before an action and run in order to determine whether or not to continue processing the request. The most common use-case for policies is to restrict certain actions to logged-in users only.
App Structure
An interactive guide to the structure of the default app.
├── app
│ ├── src
│ ├── api
│ ├── controllers
│ ├── policies
│ ├── config
│ ├── env
│ ├── development.env
│ ├── local.env
│ ├── production.env
│ ├── bootstrap.js
│ ├── cors.js
│ ├── http.js
│ ├── index.js
│ ├── log.js
│ ├── policies.js
│ ├── routes.js
├── .editorconfig
├── .eslintrc
├── .gitignore
├── .npmrc
├── .prettierrc
├── README.md
├── app.js
└── package.json