0.1.0 • Published 8 years ago

thanst-router v0.1.0

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

thanst-router:

Lightweight routing module for NodeJS web application

A minimal application:

'use strict';

const http = require('http');
const Router = require('thanst-router');

let routes = new Router('/'); // Passing in base url

routes.get('/', (req, res) => {
  homepageGet(req, res);
});

function homepageGet(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.write('This is homepage');
  res.end();
}

http.createServer(routes.init()).listen(3000, () => {
  console.log('Server running at port 3000');
});

Handling url's query string. For e.g http://example.com/articles?id=11111:

'use strict';

const url = require('url');

router.get('/articles', (req, res) => {
  let id = url.parse(req.url, true).query.id;
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.write('This article\'s id is', id);
});

Custom 404 handling:

// Simply rewrite the notFound404 class method
router.notFound404 = (req, res) => {
  res.writeHead(404, {
    'Content-Type': 'text/plain'
  });
  res.write('Custom 404');
  res.end();
};
0.1.0

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago