Icore
is a framework a high-level to build strict web application. Allows the developer to focus on the final handler.
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
To use iсore
in your project, run:
npm i icore
- 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
-
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
});
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
- returns: <Promise> Following successful listen, the Promise is resolved with an value with a
undefined
.
Stops the server from accepting new connections.
In this variable, an instance of the server will be assigned after a successful listen installation.
This empty <Object>. You may add additional properties to inq.context by editing app.context
.
app.context.db = db();
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.
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.
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!'
};
}
});
Uses the same interface as the class constructor.
const catalog = homepage.route({
method: 'catalog',
async handler(inq) {
return {};
}
});
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.
- <String> The property of the method can be any valid HTTP method in lower case letters. Default:
'get'
.
- <AsyncFunction> takes an instance of a class Inquiry as context. The handler option must return a object, a promise, or throw an error.
- <Boolean> If set to
true
, the find returns the current route. Default:false
.
- <Array> Nested route list.
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.
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.
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();
}
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);
}
- <Object> Get raw query string void of
?
.
- <String> The property of the method can be any valid HTTP method in lower case letters. Default:
'get'
.
- <Object> Response header object.
- <Object> Inherit app.context