npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

blockbase-express

v0.2.10

Published

blockbase driver for Express

Downloads

6

Readme

Driver Express for Blockbase

Compatible with Blockbase Framework

Version

0.2.8

How to install ?

$ npm i --save blockbase-express

Then add to your config/{env}.yml the following (example) instructions depending of your system

express :
    port: 1340
    open: true
    silent: false
    body_parser_limit : 50mb
    404_redirect : /404
    routes :
        - type: controller
          src: /foo/bar
          dest: /controllers/foo::bar
          method: get
        - type: view
          src: /
          dest: home
          method: get

(optional) You can also register routes in a seperated file config/routes.yml :

- type: controller
  src: /foo/bar
  dest: /controllers/foo::bar
  method: get
- type: view
  src: /
  dest: home
  method: get

Options

  • port : the port you want the server to be listening on. (default : 4000)
  • open : open the app in your browser when the app starts (default : true)
  • silent : disable error message when No custom error handlers are used (default : false)
  • body_parser_limit : content size limit to be parsed by Bodyparser
  • 404_redirect : URL to redirect a user to when requesting a URL that doesn't exist
  • assets : your project assets folder, at your project's root directory (ex : 'src/views/public') (default : 'views/assets')
  • async_init : init drivers and models first, but not the controllers to allow you to do it manually (ex : if you want to use express.use(morgan()) ) (default : false)
  • session_secret : your express-session secret key
  • session_redis_host : your express-session host to your redis server (default localhost)
  • session_redis_port : your express-session port to your redis (default 6379)

Usage

The entire usage of the driver is done by the config/{env}.yml, we've tried to make it as simple as it need.

Ports

The port is simply handled by the config/{env}.yml file in the port section of express.

express :
    port: 1340
    #...

The example above creates a server on http://localhost:1340

Routing

A route can have two type : controller or view. As it looks like a controller will be a program call (in a app.controller.* controller) and a view will call a view in the folder /views.

The following routes are programmed as described below :

  • controller ex : creates a route on localhost:1340/foo/bar that will trigger the app.controllers.foo.bar() method on the GET method
      - type: controller
        src: /foo/bar
        dest: /controllers/foo::bar
        method: get
  • view ex : creates a route on localhost:1340/ that will show the /views/home.twig template on the GET method
      - type: view
        src: /
        dest: home
        method: get

Middlewares

A middleware needs to have a destination dest, and can optionaly have a source src to define the path where to use it. Example :

    - dest: uri-logger
    - dest: cors
      src: /login
    #...

Then, inside the /middleware folder, create a middleware using the following sample :

//cors.js - /login
module.exports = (app) => {
    return function(req, res, next) {
        app.drivers.logger.log(`hello it's Middleware again on /login !`)
        next() //Continue to next middleware/route call
    }
}

Static Assets

Static assets can be stored in /views/assets/* You can then call them directly from the route localhost:port/assets/*

You can override this outside route by adding the assets route config to the express configuration

express :
    assets: /static
    #...

Body Parser

The [https://www.npmjs.com/package/body-parser](body parser) is a critical sub-library really useful when creating APIs, it handles the JSON support of the route and will create a security layer on the size of your requests.

You can set up the bodyparser limit with the body_parser_limit parameter. To see what to put inside this key please refer to the [https://www.npmjs.com/package/body-parser#limit](lib doc section).

express :
    body_parser_limit: /50mb
    #...

Redirects

The driver handles automatically 404 redirections to improve security and SEO compliance. Just add the route you wanna you for your 404 in the 404_redirect section

express :
    404_redirect: /notfound
    #...
    routes :
        #...
        - type: view
          src: /notfound
          dest: fourOfour
          method: get
    #...

The example above will redirect all 404 responses to /notfound handled by the four0four html template.

Async Init

Due to Blockbase architecture, drivers & models are created before controllers. However if you use controllers related routes, you might need to force Express to wait until the controllers are ready to listen & use addons.

In order to do that, two steps :

Add the boolean async_init to the config/{env}.yml in the express section :

express :
    async_init : true
    #...
    routes :
        #...
        #...

Then manually trigger app.drivers.express.route and app.drivers.express.listen after the app init in the main blockbase callback.

blockbase({ root : __dirname }, (app) => {
    app.drivers.express.route()
    app.drivers.express.listen()
})

Sessions

blockbase-express driver includes a native support of the express-session driver (with Redis).

To activate it, you just have to fill the following infos :

express :
    session : true
    session_secret : hereYourSecretKey
    session_redis_host : host to your redis server (default localhost)
    session_redis_port : port to your redis (default 6379)

The session_secret key is mandatory in order to secure your sessions. Try to use a cool rock-solid hash :)

Be also careful redis is mandatory when you use session...

Issues

If you find any issue, feel free to post it in the repo on Github

License

(Licence MIT) Coded by Blacksmith

Free Software, Hell Yeah!