1.1.9 • Published 6 years ago

loopsailor v1.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

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

  • Any files saved under the /config/env/<environment-name>.env folder will be loaded only when LoopSailor is served in the NODE_ENV=<environment-name>. For example, files saved under config/env/production.env will only be loaded when LoopSailor is served with NODE_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 KeyPurpose
corsCORS is like a more modern version of JSONP-- it allows your application to circumvent browsers' same-origin policy. By default is set to true.
handler404This is an 404 handler for use in both developmend and production environments. By default is set to true.
handlerErrorThis 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 KeyPurpose
bodyParserParses parameters and binary upstreams (for streaming file uploads) from the HTTP request body.
compressionCompresses response data using gzip/deflate. See compression for details.
cookieParserParses the cookie header into a clean object for use in subsequent middleware and your application code.
expressHystrixHystrix component proved to be really useful in the service client pipeline to follow fail fast pattern as well as provide real-time metrics.
helmetHelmet helps you secure your Express apps by setting various HTTP headers.
XGlobalTransactionIdAttaches 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
PriorityLevelLog fns that produce visible output
0offN/A
1error.error()
2warn.warn() .error()
3info.info() .warn() .error()
4debug.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

app.js

api/controllers

api/policies

config/env

config/boostrap

config/http

config/index

config/log

config/policies

config/routes