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

sinodetra

v0.0.4

Published

Minimal Sinatra like router for node.js

Downloads

1

Readme

Sinodetra

Minimal Sinatra like router for node.js

Installation

npm install sinodetra

Usage

At first, (install and) require Sinodetra within your script and specify your preferred port (8000 in this example).

var app = require('sinodetra')(8000);

That’s it, you’re able to add routes now. Sinodetra extends node’s HTTP server by providing get, post, put, and delete methods. You can add routes like this.

app.get('/', function(request, response) {
	response.plain('Hello world!');
});

app.get('/user/:user', function(request, response, user) {
	response.html('Hello, <strong>' + user + '</strong>');
});

app.get('/show/([0-9]+)', function(request, response, id) {
	response.html('Hello, <strong>#' + id + '</strong>');
});

As you can see, you can also use regular expressions within your route definitions. For a better readability, Sinodetra provides the opportunity to use placeholders (see :user above). However, these placeholders are nothing but a regular expression in the end.

In addition, Sinodetra extends node’s HTTP ServerResponse by providing html, plain, and json methods. These methods are actually shortcuts for the likewise provided send method, which allows you to customize the HTTP content type and status code for your response.

response.send('Hello World', 200, 'text/html');

Using response.send() you can set the HTTP content type and status code to whatever you need.

And—last but not least—there’s a way to define a callback if no routes match the requested path. This is normally used to display some fancy 404 error.

app.error(function(request, response) {
	response.plain('404 Not Found');
});

Parameters

When you’re building applications using Sinodetra, you’ll need to get information from your users. Kindly, grabbing data from the request is an easy enough process. You can access all parameters by using the request.params object.

app.get('/test', function(request, response) {
	response.plain('ID = ' + request.params.id);
});

Alternatively, you can use the provided request.param method, which returns the parameter in case it was provided or false otherwise.

app.get('/test', function(request, response) {
	response.plain('ID = ' + request.param('id'));
});

Sinodetra will try to detect what kind of data was sent within a request. If it’s JSON, request.body will be the parsed JSON object. Otherwise, Sinodetra assumes that the information was passed as application/x-www-form-urlencoded and parse the query string.

Assuming you execute a POST request containing demo=Lorem&id=Ipsum to /test?id=123, Sinodetra will merge those parameters, using GET parameters as the base and overwriting existing keys with the data from the original methods. Thus, the previous example would result the following request.params object.

{
    "demo": "Lorem",
    "id": "Ipsum"
}

Please note that Sinodetra has no nice way to handle file uploads (yet!).

Examples

app.get('/', function(request, response) {
	response.plain('Hello world!');
});

app.get('/say/:greeting/to/:person', function(request, response, greeting, person) {
	response.html('<b>' + greeting + '</b>, ' + person + '!');
});

app.get('/([0-9]+)/plus/([0-9]+)', function(request, response, one, two) {
	response.plain((parseInt(one, 10) + parseInt(two, 10)).toString());
});

app.error(function(request, response) {
	response.plain('404');
});

Changelog

  • 0.0.4
    • Fix README
  • 0.0.3
    • Add parameter support
  • 0.0.2
    • Fix error callback
  • 0.0.1
    • Initial version

TODO

  • Documentation
  • Tests

License

Copyright (c) 2015 fapprik
Licensed under the MIT license.

See LICENSE for more info.

Contributors