Git Product home page Git Product logo

vaxic's Introduction

vaxic logo


The ultra-light superpower Node web framework

GitHub | NPM

Install

npm install --save vaxic

Basic usage

const Vaxic = require('vaxic')

const app = new Vaxic()

app.add('GET', '/getEndpoint', (req, res) => {
	res.writeHead(200)
	res.end('Hello.')
})

app.listen(80, '0.0.0.0')

Extensions

Extensions are super powerful overlays you can add onto your app to add new functionality!

Two built-in extensions exist. One is called static. It can be used to serve static files.

app.use(Vaxic.static('/site'))

The other built-in extension is called route. It can be used to route requests.

app.use(Vaxic.route)

app.add('POST', async (req, res) => {
	try {
		await res.route({
			'origin': 'http://localhost:5135'
		})
	}
	catch (err) {
		res.writeHead(500)
		res.end('Failed to route!')
	}
})

Handles

Handles are methods you provide to be used as request handlers for specific requests.

You can target them by request method or by URL (or both or neither!)

Creating handles is as easy as...

app.add('POST', (req, res) => {
	// This handle handles all POST requests.

	console.log(req.body)
})

Regular expressions in handles

Regular expressions can be used as handle URL targets!

app.add('GET', /^\/api/, (req, res) => {
	// This handle handles all GET requests to /api and all subpaths of /api!
})

The Request and Response classes, extended

Vaxic request and response objects passed to handlers extend the http.ClientRequest and http.ServerResponse objects.

How Vaxic changes ClientRequest

Vaxic adds the body property to requests which contain a body. (Ex. POST requests with bodies.)

Vaxic changes the url property of ClientRequest by URL parsing it into a URL object. (Without its querystring parsed.)

How Vaxic changes ServerResponse

Vaxic adds the endGzip, endDeflate, and endCompressed methods to the http.ServerResponse object.

ServerResponse.endGzip(body, statusCode, ?headers, ?cb)

ServerResponse.endDeflate(body, statusCode, ?headers, ?cb)

ServerResponse.endCompressed(body, ?statusCode:200, ?headers, ?cb)

All of the compression methods add the appropriate content-encoding header. Use endCompressed to autodetect the preferred compression method of the client based on the accept-encoding header.

Using another HTTP server package (such as HTTPS)

If you'd like to use your Vaxic app with another HTTP server such as Node's built-in HTTPS module, you can do so using app.serverHandler.

For example:

const https = require('https')

https.createServer(app.serverHandler).listen(80)

Creating extensions

Making extensions is easy! Extensions are just methods to which requests are passed before (or instead of) being handed over to handles.

(req, res, next) => {
	res.setHeader('Powered-By': 'Vaxic-Engine')
	next()
}

Calling next() in extension handler methods is important because it allows the request to propagate to the next applicable handler. (An extension or handle.)

Async / Promise handler methods for handles and extensions

Async or Promise-returning functions may be used as handlers in handles and extensions.

If a promise returned by a Promise-based handler is rejected, the rejection will be caught and a promiseHandleRejection (for handles) or a promiseExtensionRejection (for extensions) will be emitted with the error from the Vaxic instance.

Ex. for Async handle handlers:

app.add('GET', '/async', async function (req, res) {
	// Perform await / other logic if desired and handle request.
})

Ex. for async extension handlers:

app.use(async function (req, res, next) {
	// Perform await / other logic if desired and handle request.
})

vaxic's People

Contributors

ethanent avatar

Stargazers

Ioan Rîpan avatar Emiliano Mastragostino avatar HAKASHUN avatar Reed avatar Daniel Mello avatar Linus avatar Sam Stuart avatar Tom Hummel avatar HippoDippo avatar Colton Colcleasure avatar Alex Lemanski avatar Victor Hugo Bernardes de Souza avatar Roy Li avatar Fabrizio Fortunato avatar Brian Gershon avatar Ganeshan Venkataraman avatar Nick Meincken avatar Fabian Morón Zirfas avatar Kotaro Yoshimatsu avatar Anderson Araujo avatar Georgii Rychko avatar C.J. Winslow avatar  avatar Mike Repeć avatar Andrew Chou avatar Dani Sandoval avatar Daviot avatar yury avatar Michael Kovner avatar Benjamin Nwaneampeh avatar Jesse Hattabaugh avatar Marco Campos avatar hkawaida avatar Shawn Jonnet avatar Christophe avatar C Dorn avatar  avatar Róbert Oroszi avatar Chris Coggburn avatar Byungjik Roh avatar Lucas Aragno avatar Bad Mark avatar

Watchers

yury avatar Linus avatar  avatar  avatar

Forkers

ez-vindi

vaxic's Issues

Optional cookie support

Hello, nice job with this framework, minimalism at it's finest.

But one question though, why do you have a dependency called lightcookie in this project? In my case, I don't really need to handle cookies in my projects, so I guess you could make this cookie support as an extension or plugin, so you could promote this as a dependency-free web framework.

As a workaround I just delete the line 24 and then delete the module require statement at line 5.

Thank you.

Use random port number?

How would I do this in Vaxic? With Express, I could do this:

const http = require("http");
const express = require("express");
const app = express();

app.use("/", serveStatic("./public"));

app.get("/", (req, res) => {
    res.render("index");
});

const server = http.createServer(app);

server.listen(0, () => {
	console.log(server.address().port);
});

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.