Giter VIP home page Giter VIP logo

pomeranian-js's Introduction

NPM Version NPM Downloads Linux Build Coverage Status

Pomeranian.js

Hello buddy! This is another light-weight high performance, extendable solution for you to build modern, scalable Node.js applications. It's similar to what you have already seen, to get details please read this documentation.

Installation

npm install pomeranian-js --save

or

yarn add pomeranian-js

Hello World

Just a quick example how you can start.

import { Application, Router } from "pomeranian-js";

const app = new Application();
const router = new Router();

router.addRule(
  { method: "GET", url: "/" },
  (req, res) => {
    res.json(200, { status: "Success" });
  }
);

app.useRouter(router);

app.start({
  port: 3000,
  callback: () => {
    console.log("Server is running...");
  }
});

Middlewares

Basically this is a simple way to do something with req and res objects while processing client's requests, e.g. add authorization logic before API callback runs. I call this feature as layers.

You can define layers using two ways:

Local

For specific route rule (route-level):

router.addRule(options, callback, (req, res, next) => {
  // do something with "req" and "res" objects and run callback
  next();
});
Global

Will be executed for all routes (app-level):

app.useMiddleware((req, res, next) => {
  // do something with "req" and "res" objects and run callback
  next();
});

Routes

For adding a new routing rule, you should use addRule method:

router.addRule({
  method: String, // default GET
  url: String,
  match: Object,
  query: Object,
  fileParsing: Boolean // default false
}, (req, res) => {});
Options
  • method - HTTP method, can be GET, POST, PUT, DELETE (optional)
  • url - pattern for request url (required)
  • match - patterns for special parameters in request url (optional)
  • query - patterns for query string parameters, after question mark (optional)
  • fileParsing - framework parses (if true) or doesn't parse (if false) request's body for uploaded files if Content-Type is multipart/form-data (optional)
Callback

This is how you can handle client's requests.

You can do it with typical way:

router.addRule(options, (req, res) => {
  res.statusCode = httpCode;
  res.end(content);
});

Or using our methods out of the box (res.html, res.json, res.redirect):

router.addRule(options, (req, res) => {
  res.html(httpCode, html); // return HTML content
});
router.addRule(options, (req, res) => {
  res.json(httpCode, json); // return JSON object
});
router.addRule(options, (req, res) => {
  res.redirect("/path/", 301); // redirect user to another page or website
});

Routes Examples

Just a few examples how you can use it:

router.addRule({
  url: "/{category}",
  match: {
    category: ["phones", "tablets"]
  }
}, (req, res) => {
  res.json(200, req.params);
});
router.addRule({
  url: "/{category}/{name}",
  match: {
    category: ["phones", "tablets"],
    name: "([a-z0-9]{3,50}"
  }
}, (req, res) => {
  res.json(200, req.params);
});
router.addRule({
  url: "/{category}/{name}",
  query: {
    password: "[a-z0-9]{3,50}"
  }
}, (req, res) => {
  res.json(200, { ...req.params, ...req.query });
});

Variables

While processing client's request you can get access to internal variables in req object:

  • req.clientIpAddress - client's IP address
  • req.referer - client's referrer, identifies URL that linked to resource being requested
  • req.params - URL parameters
  • req.query - query string parameters
  • req.files - name, extension, mime and content of uploaded file

Advices

We collected some advices for you, it can be useful in some cases.

Page not found

If some client's request doesn't match your routing rules, our framework will shows blank page with 404 http status. Of course for production we need more intelligent solution, so here is example how you can show your custom "not found" page:

router.addRule({
  url: "/{url}",
  match: {
    url: "(.*)"
  }
}, (req, res) => {
  res.html(404, content);
});

Just need add new routing rule for processing all requests. Important thing: this rule must be last one - just in case to overwrite previous, it's very important.

Contributing

You can help to improve my framework, here is a lot of work to do:

  • review pull requests
  • find new issue or fix existing
  • add new feature or improve old
  • update documentation

License

The Pomeranian JS framework is open-source software licensed under the MIT

pomeranian-js's People

Watchers

Ihor Kirei avatar

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.