icore
v0.1.38
Published
High-level framework for creating strict web applications
Downloads
94
Readme
Iсore
Icore
is a framework a high-level to build strict web application. Allows the developer to focus on the final handler.
Features
This module for Node.js® implemented by following the ECMAScript® 2018 Language Specification Standard
- strict interface
- not immutable context
- routing implements a binary tree
- all asynchronous functions on promises
- error handling
Install
To use iсore
in your project, run:
npm i icore
Table of Contents
- Event: 'error'
- constructor: new Route(options)
- route.route(options)
- route.find(paths[, index])
- route.method
- route.handler
- route.finish
- route.childs
- constructor: new Inquiry(req, cookie)
- inq.read(options)
- inq.pipe(stream[, options])
- inq.queries
- inq.method
- inq.headers
- inq.context
class Application
constructor: new Application(options)
options
<Object>host
<String> To start the development server using a different defaulthostname
orIP address
. This will start a TCP server listening for connections on the provided host. Default:'localhost'
.port
<Number> TCP port of remote server. Is required parameter.timeout
<Number> The number of milliseconds of inactivity before a socket is presumed to have timed out. A value of0
will disable the timeout behavior on incoming connections. Default:12e4
(2 minutes).maxHeadersCount
<Number> Limits maximum incoming headers count. If set to0
, no limit will be applied. Default:2e3
.
For example:
const icore = require('icore');
const app = new icore.Application({
port: 3000
});
app.listenHttp(router)
router
<Router> Is instance class Route this module.- returns: <Promise> Following successful listen, the Promise is resolved with an value with a
undefined
.
Starts the HTTP server listening for connections. These arguments are documented on nodejs.org.
index.js
const icore = require('icore');
const router = require('./router');
const app = new icore.Application({
port: 3000
});
app.listenHttp(router);
Start script in terminal:
$ node index.js
Server started on http://localhost:3000
app.close()
- returns: <Promise> Following successful listen, the Promise is resolved with an value with a
undefined
.
Stops the server from accepting new connections.
app.server
In this variable, an instance of the server will be assigned after a successful listen installation.
app.context
This empty <Object>. You may add additional properties to inq.context by editing app.context
.
app.context.db = db();
class Route
The router is made up of instances class Route
.
Routing will be done:
| Method | URL | |--------|----------------------------------| | GET | http://example.com | | GET | http://example.com/catalog | | GET | http://example.com/logs_0 | | GET | http://example.com/logs_1 | | POST | http://example.com/catalog/books |
Extends the Node.js events module.
Event: 'error'
The 'error'
event is emitted if throw an exception. The listener callback is passed a single Error
argument when called.
The server is not closed when the 'error'
event is emitted.
constructor: new Route(options)
When defining a route in icore
you need one basic element - the handler
.
options
<Object>handler
<AsyncFunction> takes an instance of a class Inquiry as context. The handler option must return a object, a promise, or throw an error.path
<String> The path option must be a string, though it can contain regular expression. Default:''
.method
<String> The property of the method can be any valid HTTP method in lower case letters. Default:'get'
.finish
<Boolean> If set totrue
, the find returns the current route. Default:false
.
Simple example use Route class:
const homepage = new Route({
async handler(inq) {
return {
status: 200,
header: {
'Content-Type': 'text/html; charset=utf-8'
},
body: 'Hello World!'
};
}
});
route.route(options)
Uses the same interface as the class constructor.
const catalog = homepage.route({
method: 'catalog',
async handler(inq) {
return {};
}
});
route.find(paths[, index])
This method is intended for class Application use.
paths
<Array> The result of the transform of the URL.index
<Number> Is cursor for next call find. Default:0
.- returns: Returns
null
if the route is not found.
route.method
- <String> The property of the method can be any valid HTTP method in lower case letters. Default:
'get'
.
route.handler
- <AsyncFunction> takes an instance of a class Inquiry as context. The handler option must return a object, a promise, or throw an error.
route.finish
- <Boolean> If set to
true
, the find returns the current route. Default:false
.
route.childs
- <Array> Nested route list.
class Inquiry
A class Application context encapsulates node's request and response objects into a single object which provides many helpful methods for writing web applications and APIs.
constructor: new Inquiry(req, cookie)
The constructor will be auto called whenever a new request
is established.
req
<http.IncomingMessage> Node's request object.cookie
<coookie.entries> The values of the incoming cookie.
inq.read(options)
options
<Object>maxSize
<Number> Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted. Default:Infinity
.
- returns: <Promise> Following successful listen, the Promise is resolved with an value with a
string
type.
To start reading the body of the incoming message.
async handler(inq) {
const data = await inq.read();
}
inq.pipe(stream[, options])
stream
<stream.Writable>options
<Object>maxSize
<Number> Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted. Default:Infinity
.
To start reading the body of an incoming message as a stream
async handler(inq) {
inq.pipe(writeStream);
}
inq.queries
- <Object> Get raw query string void of
?
.
inq.method
- <String> The property of the method can be any valid HTTP method in lower case letters. Default:
'get'
.
inq.headers
- <Object> Response header object.
inq.context
- <Object> Inherit app.context