1.0.1 • Published 5 years ago

ar-router v1.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

Royter

Simple url router, to create API endpoints and server files and static folders, made in Node.JS.

I created this to learn the basics of Node, it is not built to be used in production code.

Usage

Initilise

Install package

npm i ar-router

Then create Router

let arRouter = require("ar-router");
let router = new arRouter.Router();

Handle

http.createServer(function(req, res){
	router.handle(req, res);
}).listen(8080);

Simple API

router.register({

	path: "/api/something",
	callback: function(routeParams){
		return "This will get returned as JSON
	},

});

API Router Params

router.register({

	path: "/api/something/:name",
	callback: function(routeParams){
		console.log(routeParams);
		return "Hello " + routeParams.name;
	}

});

Register Page

// Serve homePage.html at /home
router.registerPage("/home", "homePage.html");

Register Folder

// Serve contents of /resources/** folder at /res/**
router.registerStaticFolder("/res", "resources");

Additional API Route Configuration

router.register({

	requestMethod: arRouter.RequestMethod.POST, // This defaults to GET (can also use "GET")
	path: "/api/something/:name",
	callback: function(routeParams){
		console.log(routeParams);
		return "Hello " + routeParams.name;
	},
	caseInsensitive: false // This defaults to true

});