1.0.7 • Published 2 years ago

pomeranian-js v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago