Node.js Web development framework -Titbit

Node.js Web development framework -Titbit

2022-09-05 0 827
Resource Number 38081 Last Updated 2025-02-24
¥ 0USD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

The recommended titbit in this issue is a Web backend framework for the node.js environment, supporting HTTP/HTTPS/HTTP2, and supporting configuration switching. Provides middleware and grouping mechanism. And provides many extensions for quickly building services.

Node.js Web development framework -Titbit插图

titbit core function

  • Request context design masks interface differences.
  • Middleware pattern.
  • Route grouping and naming.
  • The middleware groups the execution according to the route.
  • Middleware matches request method and routing execution.
  • Start the daemon process: use the cluster module.
  • Displays the load of the child process.
  • body data is parsed by default.
  • Support for enabling HTTP/1.1 or HTTP/2 services through configuration.
  • Enable the HTTPS service (HTTPS must be enabled for HTTP/2 services).
  • Limit the number of requests.
  • Limits the maximum number of accesses to a single IP over a period of time.
  • IP blacklist and IP whitelist.
  • In cluster mode, the monitoring child process restarts if it exceeds the maximum memory limit.
  • Automatic load mode is optional: a new child process is created according to the load to handle the request, and the initial state is restored when idle.

Installation

npm i titbit

Can also be installed using yarn:

yarn add titbit

Example: Add a route

'use strict'

const titbit = require('titibit')

const app = new titbit()


app.get('/', async c =>  {
//data is of type string|Buffer. You can set c.res.encoding to the encoding of the returned data. The default is 'utf8'. 
c.res.body = 'success'
})

// The default listener is 0.0.0.0, with the same parameters as the native interface listen. 
app.run(1234)

Route and request type

The starting line of HTTP gives the request type, also known as: Request method. Current request method:

GET POST PUT DELETE OPTIONS  TRACE HEAD PATCH

示例:


'use strict';

const titbit = require('titibit');

const app = new titbit({
  debug: true
});

app.get('/', async c => {
  c.res.body = 'success';
});

app.get('/p', async c => {
  c.res.body = `${c.method} ${c.routepath}`;
});

app.post('/', async c => {
  // Return uploaded data 
  c.res.body = c.body;
});

app.put('/p', async c => {
  c.res.body = {
    method : c.method,
    body : c.body,
    query : c.query
  };
});

// The default listener is 0.0.0.0, with the same parameters as the native interface listen. 
app.run(8080); 

Get URL parameters and form data

  • Query string in the URL (? a=1& Arguments of the form b=2) are resolved into c.query.
  • The form submission data is parsed into C.bDY.

The content-type of the form is application/x-www-form-urlencoded

'use strict';
const titbit = require('titbit');

var app = new titbit();

var {router} = app;

router.get('/q', async c => {
  // in the URL? The subsequent query string is parsed into query. 
  c.res.body = c.query; // Returns JSON text, the main difference is that the content-type in the header is text/json
});

router.post('/p', async c => {
  //POST, PUT the submitted data is saved to the body, if it is a form it is automatically parsed, otherwise just the original text value is saved, 
  // Middleware can be used to process a variety of data. 
  c.res.body = c.body;
});

app.run(2019);

body Maximum data size limit

'use strict'

const titbit = require('titbit')

const app = new titbit({
  // The maximum amount of data allowed for a POST or PUT request is approximately 20M. 
  // The unit is bytes. 
  maxBody: 20000000
})

//...

app.run(1234)

Middleware

Middleware is a very useful pattern, which is somewhat different in different languages, but in essence there is no difference. The operation mechanism of middleware allows developers to better organize code and facilitate the implementation of complex logic requirements. In fact, the entire framework runs on middleware patterns.

Node.js Web development framework -Titbit插图1

At the design level, the middleware of this framework can also identify different request types according to route groups, and determine whether to execute or skip to the next layer, so the speed is very fast, and multiple routes and groups have their own middleware, which does not conflict with each other, and will not make meaningless calls. The reference form is as follows:


/*
The second parameter can be left blank, indicating that the middleware is enabled globally.
Now the second parameter indicates that only the POST request method is executed, and the route grouping must be /api.
Based on this design, it can be guaranteed to perform on demand without doing too many meaningless operations.
*/
app.add(async (c, next) => {
    console.log('before');
    await next();
    console.log('after');
}, {method: 'POST', group: '/api'});

titbit完整的流程图示

Node.js Web development framework -Titbit插图2

Log

The framework itself provides the global logging function, when using cluster mode (using daemon interface to run the service), using the initialization option globoalLog can enable global logging, and can specify the log file, in single process mode, the log will be output to the terminal. At this point the log can still be saved to a file using output redirection and error output redirection.

Message event processing

Based on message events, in daemon mode (based on the cluster module), a setMsgEvent function is provided to obtain and process event messages sent by child processes.

This requires that the message sent by the worker process must be an object, where the type attribute is required, representing the name of the message event. The data in other fields can be customized.

Other

  • titbit will have a last-wrapped middleware to do the final processing after running, so setting the value of c.res.body will return data. The default will detect some simple text type and automatically set the content-type (text/plain, text/HTML, application/json). Note that this is done without setting the content-type.
  • limits the maximum url length by default, and also sets a maximum memory usage depending on the hardware.
  • All of this can be extended and overwritten by configuration options or middleware, with both limitations and freedom.
资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free source code Node.js Web development framework -Titbit https://ictcoder.com/kyym/node-js-web-development-framework-titbit.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service