GithubHelp home page GithubHelp logo

coggers's Introduction

COGGERS

Coggers

Coggers is a object-based web server, boasting amazing modularity.

Blueprints

Coggers uses "blueprints" to define what a server should look like.

For example, where in express you'd use

import express from "express";
const app = express();
app.get("/users/:id/", (req, res) => {
	const user = database.getUser(params.id);
	res.send(user);
});
app.post("/users/:id/", (req, res) => {
	const user = database.createUser(params.id);
	res.send(user);
});
app.listen(8080, () => console.log("Listening!"));

in Coggers you instead use

import { Coggers } from "coggers";
const coggers = new Coggers({
	users: {
		":id": {
			$get(req, res, params) {
				const user = database.getUser(params.id);
				res.send(user);
			},
			$post(req, res, params) {
				const user = database.createUser(params.id);
				res.send(user);
			},
		},
	},
});
coggers.listen(8080).then(() => console.log("Listening!"));

Middleware

In Coggers, middleware does not have a next() function. More info

Anyhow, to add middleware you can use the $ key, with an array of middlewares.

Note Coggers comes with cookie-parsing built in, so the cookie-parser middleware isn't necessary. This is just to show an example of how you can define middleware.

import { express } from "coggers/compat";
import cookieParser from "cookie-parser";
const coggers = new Coggers({
	// express() turns express-based middleware into coggers middleware.
	$: [express(cookieParser())],
	$get(req, res, params) {
		const user = database.getUser(req.cookies.id);
		res.send(user);
	},
	$post(req, res, params) {
		const user = database.createUser(req.cookies.id);
		res.send(user);
	},
});

Modularity

As Coggers is fully object-based, modularity is simpler than ever. All you need to do is export a part of a blueprint from file A, and then you can import that in file B and simply put it right in.

Example

Intellisense

Because of this modularity, it's likely that there's blueprints that aren't passed directly into the Coggers constructor. (In the last example, the variable users.)

For your IDE to be able to autocomplete blueprints, you can use the blueprint() utility function. (don't worry, it won't do anything with your blueprint!)

import { blueprint } from "coggers";
const blue = blueprint({
	// Woah, intellisense
});

In typescript, you can also annotate your variable with the Blueprint type:

import { Blueprint } from "coggers";
const blue: Blueprint = {
	// Woah, intellisense
};

Render Engines

Render engines are defined using the renderEngine middleware. Full example here

import { Coggers, renderEngine } from "coggers";
import { renderFile } from "poggies";
const viewsDirectory = new URL("views", import.meta.url);
const coggers = new Coggers({
	// Poggies files end with ".pog", so "pog" here ⬇️
	$: [renderEngine(renderFile, viewsDirectory, "pog")],
	$get(req, res) {
		res.render("index", {
			random: Math.random(),
		});
	},
});
Middleware note

Asynchronous middleware is expected to either be defined as an async function, or to return a promise. If a middleware doesn't want to continue to the next handler, it has to send something to the client (using res.sendStatus(<number>), res.end(), etc.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.