1.1.0 • Published 2 years ago

active v1.1.0

Weekly downloads
13
License
ISC
Repository
-
Last release
2 years ago

NPM Version NPM Downloads Linux Build Coverage Status

ActiveJS

High performance, extendable solution with less unnecessary features. Achieve the maximum possible speed of NodeJS framework!

Docs

Installation

$ npm install active --save

New Application

const active = require("active");
const app = active();

http.createServer(app).listen();

Configuration

Using next method you can change application settings. All settings are optional.

app.tune({
    cors: Boolean, // default false
    debug: Boolean // default false
});
Parameters
  • cors - cross-origin resource sharing, read details here
  • debug - application with enabled debug mode prints speed for each request

Routes

For adding new routing rule, you should use addRoute method:

app.addRoute({
    method: String, // default GET
    url: String,
    match: Object,
    query: Object,
    fileParsing: Boolean // default true
}, (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:

app.addRoute(options, (req, res) => {
    res.statusCode = http_code;
    res.end(content);
});

Or with custom way which is provided by ActiveJS:

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

Few examples how you can use it:

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

Variables

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

  • req.client_ip - 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
  • req.route - current route rule

Middleware

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. In ActiveJS we know this feature as layers.

You can define layers using two ways:

Specific

Will be executed for request matched specific route rule:

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

Will be executed for each request (all routes):

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

If you want to use few layers, you must send array with functions, instead of one function:

app.addRoute(options, [Function, Function, Function], callback);
app.useLayer([Function, Function, Function]);

Tips

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:

app.addRoute({
    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.

Tests

After installing of all dependencies you can run tests:

npm test

Contributing

You can help to improve "Active" framework, there is lot of work to do:

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

License

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

1.1.0

2 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.5.6

7 years ago

0.5.5

8 years ago

0.5.4

8 years ago

0.5.3

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

9 years ago

0.4.9

9 years ago

0.4.8

9 years ago

0.4.7

9 years ago

0.4.6

9 years ago

0.4.5

9 years ago

0.4.4

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.9

9 years ago

0.3.8

9 years ago

0.3.7

9 years ago

0.3.6

9 years ago

0.3.5

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.9

9 years ago

0.2.8

9 years ago

0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.9

9 years ago

0.1.8

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago