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

webby.js

v0.2.6

Published

Microframework for express

Downloads

20

Readme

WebbyJS

What is WebbyJS

WebbyJS is a micro framework to quickly create simple web applications built on top of express.js

Install

Just run npm install webby.js --save to install webby

Example

const webby = require('webby.js');
webby(8081, __dirname + '/controllers');

Thats all there is to it!

Look in the example directory for a fully working example and be amazed at it's simplicity!

Controllers

Every .js file in the controllers directory is the first segment of the route. For example /controllers/bakery.js will hold all routes like http://localhost:8081/bakery.

The only exception to this rule is /controllers/index.js. This file will hold the root (http://localhost:8081/).

Methods

Methods in the controllers are very specific in their naming. They start with a lowercase method followed by the route name with an uppercase. For example getCookie(req, res) will serve the /cookie get request, while the postCookie(req, res) will server the /cookie post request. A post body can be filled either urlencoded or as application/json data

Index

If you want the index of the bakery.js controller to be accessable as /bakery create an getIndex(req, res) method. index is a special keyword that will always result in something without a name. The first controller will be index.js with inside of it a getIndex(req, res) method to server the root of your website

Additional Route Parameters

The first 2 parameters inside a controller functions are always express.js's reqest and response objects. After that you can add an (in)finite amount of parameters to your controllers. eg. getCookie(req, res, arg1, arg2, arg3) will allow the you to navigate to http://localhost:8081/bakery/cookie/hello/world/test and have the arguments populated with the last three route segments., in this case:

arg1 = "hello";
arg2 = "world";
arg3 = "test";

Templating

Webby.js uses swig as it's primary templating engine and can be used by defining the template_path in the options object.

const webby = require('webby.js');
webby(8081, {
    controller_path: __dirname + '/controllers',
    template_path: __dirname + '/theme'
});

Templates use almost the same naming conventions as controllers and methods, however with templates it's in the directory structure and filename. For example, if you have a controller called bakery with a method called getCookie, Webby will check if the file {template_path}/bakery/cookie.swig.html exists and use that with the data returned from the controller.

A Template file can also be overridden in the controller with the following usage:

module.exports = {
    'getIndex': {
        template: 'custom/template',
        method: (req, res) => {
            return {
                data: 'here',
            };
        }
    }
};

This will parse the {template_path}/custom/template.swig.html file with {data: 'here'} as it's data

Promises

All controller methods also support returning native Promise objects

'getIndex': (req, res) => {
    return new Promise((resolve, reject) => {
        resolve({
            data: 'here!'
        });
    });
}

Middleware

Register the middleware path in the options object: {middleware_path: __dirname + '/middleware'} Middlewares can ben defined on component and route level as follows:

module.exports = {
    'middleware': [
        'set-header',
    ],
    'getIndex': {
        ...

will be ran on every method in the component, or


module.exports = {
    'getIndex': {
        middleware: [
            'set-header',
        ],
        method: (req, res) => {
            ...

to run the middleware only on the getIndex method. set-header is the filename of the middleware.

The middleware file itself looks like:

module.exports = (req, res, next) => {

    res.header('X-Test-Header', 'Success');

    return next();
};

Static Content

Static content can be served by suppying the options.static_path to the second parameter of the webby function

const webby = require('webby.js');
webby(8081, {
    controller_path: __dirname + '/controllers',
    static_path: __dirname + '/static'
});

Sidenote

You never need to restart the server itself when working on the controllers. They are dynamically loaded on each page request. Just make your edits and refresh the page!