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

jsgram

v0.3.3

Published

A fast and lightweight framework for node with middleware

Downloads

1

Readme

jsgram

pipeline status coverage report npm npm

A fast and lightweight framework for node with middleware.

Still in beta

work in progress

Example

prints hello world

const jsgram = require("jsgram");

const app = jsgram.default();

app.get("/",(req,res) => {
	res.send("Hello World");
});

const hostname = '127.0.0.1';
const port = 3000;

app.listen(port, hostname);

Routes

  • A route has a method, a pattern, a handler and possibly middleware.

  • Methods are all Http Methods and can quickly added with the corresponding function. E. g. app.get() for http Method GET. Or methods can be manually added by calling the map function.

  • Routes can have parameters: "/:id" The parameter id can be anything.

  • Routes ca be organized in groups. Every route in a group got the same group prefix and the same middleware.

const jsgram = require("jsgram");

const app = jsgram.default();

//Get Route
app.get("/",(req,res) => {
	res.send("Hello World");
});

//Route with parameter
app.get("/:id",(req,res,id) => {
	res.send("The requested id was: " + id);
});

//post route
app.post("/post",(req,res) => {
	res.send("Hello World");
});

app.map(["POST","GET","PUT"],"/manually",(req,res) => {
	res.send("Hello World");
});

//every route in this group got the prefix /v1
app.group("/v1",() => {
	app.get("/admin",(req,res) => {
		res.send("Hello admin");
	});
	
	//nested groups are also possible
	//here with crud like routes example
	app.group("/posts",() => {
		//this route has /v1/posts as pattern
		app.get("",(req,res) => {
			res.send("Get all posts");
		});
		
		app.post("",(req,res) => {
			res.send("post added");
		});
		
		//every route in this group expected an id
		app.group("/:id",() => {
			app.get("",(req,res, id) => {
				res.send("Get post: " + id);
			});
			
			app.put("",(req,res, id) => {
				res.send("post: " + id + " successfully updated");
			});
			
			app.delete("",(req,res, id) => {
				res.send("post: " + id + " successfully deleted");
			});
		});
	});
});

const hostname = '127.0.0.1';
const port = 3000;

app.listen(port, hostname);

Middleware

  • a middleware is a function which is invoked before the route handler (e.g. a controller or a function).

  • middleware are organized as a queue: First in First out, Last in Last out.

  • all middleware get a request, response and a next function as parameters. To call the next middleware in the queue: next()

  • middleware can be:

    • global: so they will be invoked on every request
    • group: only invoked for all routes in this group
    • route: only invoked if the route is matched
const jsgram = require("jsgram");

const app = jsgram.default();

//global middleware

app.add((req,res,next) => {
	//do something before the handler
	next();	//calling the next middleware
	//do something after the handler
});

//group

app.group("/admin",() => {
	app.get("/",(req,res) => {
		res.send("Welcome user");
	});
	
	app.get("/hidden_routes",(req,res) => {
		res.send("only for authenticated users");
	});
}).add((req,res,next) => {
	//e.g. check if user is authenticated 
	//got the user info elswhere
	const user = 123;
	
	//not login -> show login page
	if(!user) {
		res.send("not login");
	}
	
	//else go to the next middleware
	next();
});

//route

app.get("/hidden_routes",(req,res) => {
	res.send("only for authenticated users");
}).add((req,res,next) => {
	next();
})

Https

import {readFileSync} from "fs";
const jsgram = require("jsgram");

const options = {
	key: readFileSync('key.pem'),
	cert: readFileSync('cert.pem')
};

const app = jsgram.default();

//Get Route
app.get("/",(req,res) => {
	res.send("Hello World");
});

const hostname = '127.0.0.1';
const port = 3000;

app.listen(port, hostname, true, options);

Async support

  • route handlers and middleware are supporting async and await operations

  • async handler can return there result instead of using res.send.

const jsgram = require("jsgram");

const app = jsgram.default();

app.add(async (req,res,next) => {
	
	await next();
	//do something after the promise is done e.g. logging
});

app.get("/async",async () => {
	return "Hello world";
});