# coggers

> we go **fast**

Latest version **1.6.2** (published 2021-12-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install coggers
pnpm add coggers
yarn add coggers
bun add coggers
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.6.2 |
| Published | 2021-12-25 |
| First published | 2021-09-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 27.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | tbhmens |

## Links

- npm: https://www.npmjs.com/package/coggers
- Repository: https://github.com/tbhmens/COGGERS
- Homepage: https://github.com/tbhmens/COGGERS#readme
- Issues: https://github.com/tbhmens/COGGERS/issues
- npm.io page: https://npm.io/package/coggers

## Dependencies (1)

- [filename2mime](https://npm.io/package/filename2mime.md) >=1.3.0

## Recent versions

- 1.6.2 (latest) — 2021-12-25
- 1.6.1 — 2021-12-25
- 1.6.0 — 2021-12-11
- 1.5.4 — 2021-12-09
- 1.5.3 — 2021-12-05
- 1.5.2 — 2021-12-02
- 1.5.1 — 2021-12-01
- 1.5.0 — 2021-11-06
- 1.4.0 — 2021-10-27
- 1.3.1 — 2021-10-16
- 1.3.0 — 2021-10-16
- 1.2.3 — 2021-10-06
- 1.2.2 — 2021-10-06
- 1.2.0 — 2021-10-04
- 1.1.1 — 2021-09-26
- … 2 more at https://npm.io/package/coggers/versions

## README

<div align="center"><img alt="COGGERS" height="120" src="./tests/assets/coggers.gif"></div>

# 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

```js
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

```js
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](#middleware-note)

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

<details>
	<summary>Note</summary>
	<small>
		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.
	</small>
</details><br>

```js
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](./examples/modularity)

### 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!)

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

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

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

### Render Engines

Render engines are defined using the renderEngine middleware. [Full example here](./examples/render-engines)

```js
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.)

---
_Source: https://npm.io/package/coggers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
