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

swrest

v0.1.43

Published

Simple routes with es6 or es5, based on express

Downloads

1

Readme

SwRest

Simple rest api for NodeJs or MeteorJs, based on express

SwRest is a simple library based on express Express.

Simple example

Code

Router.addRoute('/:name')
  .get(params => {
    const name = params.name;
    const response = {};
    response.msg = `Welcome ${name}`;
    response.status = 200;
    return response;
  })

Response

{"msg":"Welcome julio sansossio"}

Url

  • http://example.com/julio%20sansossio

Getting started

Install

npm install swrest --save

Download source

git clone https://github.com/Sansossio/SwRest.git

Available http methods

GET
POST
PUT
PATCH
DELETE
OPTIONS

Simple usage (ES5)

  const SwRest = require('swrest/es5');
  const options = { 'port': 80 };
  const Router = new SwRest.Router(options);
  Router.addRoute('/')
    .get((params) => {
      const response = { 'msg': 'ES5' };
      return response;
    });

Simple usage (ES6)

import SwRest from 'swrest';

const Router = new SwRest.Router(options);
Router.addRoute('/')
  .get(params => params);

Routes By Class (ES6)

import SwRest from 'swrest';
// Options for router
const options = {
  consolePrint: true, // Default: false
  port: 8080, // Default: 8080
  jsonParse: true, // Default: true
  allowForms: true, // Default: true
};
// Advance router with es6
class Example extends SwRest.RouterClass {
  constructor(params) {
    super(params);
    this.basePath = '/'; // Default: Class name
  }
  // Root method
  // Route url: /
  index(params, get) {
    const response = params;
    response.method = get;
    response.status = 200; // Default: 200
    return response;
  }
  // Custom method
  // Route url: /api/:id/edit
  api(params, post, id, _edit) {
    const response = params;
    response.method = post;
    response.id = id;
    response.edit = _edit;
    response.status = 200; // Default: 200
    return params;
  }
}
const example = new Example();
// Init router
const Router = new SwRest.Router(options);

// Set routes
Router.advanceRoute(example);
// Print generate routes
Router.testAdvanceRoute(example);

Example login (ES5)

  const SwRest = require('swrest/es5');
  // Login func
  function loginToken(token) {
    // False = Login fail
    // if login is true, return user info how json
    return token === '1234';
  }
  const options = {
    'port': 80,
    checkLogin: loginToken
  };
  // Set router
  const Router = new SwRest.Router(options);
  Router.addRoute('/api/:id')
    .get(params => {
      const response = params;
      const user = params.user;
      return response;
    }, [], { authRequired: true })

Not found

Router.notFound()
  .get(() => {
    return 'NotFound Get';
  })
  .post(() => {
    return 'NotFound Post';
  })
  .all(() => {
    return 'NotFound Another methods';
  });

License

MIT ©