Aller au contenu principal
Version: 3.0.0

Introduction

attention

The integrated solution will be gradually stopped for maintenance. Existing projects can continue to be used. Please choose carefully for new projects.

Midway's integrated solution is a full-stack framework based on Midway Hooks as the main function, supporting four core features: "zero" Api & type safety & full-stack suite & powerful backend.

Differences from standard projects

The integrated solution is based on the standard project, and a front-end adaptation layer is extended on top of it. While reusing all the standard project capabilities, it can also seamlessly cooperate with the front-end development, that is, in the project, both the front-end code, which in turn has Node code.

Feature introduction

Zero APIs

The back-end interface functions developed in the Midway Hooks full-stack application can be directly imported and called, without the need for handwritten Ajax glue layers at the front and back ends. Here is a simple example:

Backend code:

import {
APIs,
Post,
} from '@midwayjs/hooks';

export default Api(
Post(), // Http Path: /api/say,
async (name: string) => {
return `Hello ${name}!`;
}
);

Front-end call:

import say from './api';

const response = await say('Midway');
console.log(response); // Hello Midway!

Type Safety and Runtime Safety

Using the Validate validator provided by @midwayjs/hooks, you can achieve type-safe + runtime-safe links from front-end to back-end. Here is a simple example:

Backend code:

import {
APIs,
Post,
Validate,
} from '@midwayjs/hooks';
import { z } from 'zod';

export default Api(
Post('/hello'),
Validate(z.string(), z.number()),
async (name: string, age: number) => {
return `Hello ${name}, you are ${age} years old.`;
}
);

All-in-one call:

import hello from './api';

try {
await hello(null, null);
} catch (error) {
console.log(error.message); // 'name must be a string'
console.log(error.status); // 422
}

throughout the process.

  • Front-end: Based on type, statically verify input parameters and get type hints
  • Backend: Verify the incoming parameters of the frontend
  • Business logic such as database: use the correct data

In this way, we can achieve static type safety + runtime safety at low cost.

Full stack kit

In Midway Hooks, we provide @midwayjs/hooks-kit to quickly develop full stack applications.

You can use hooks dev to start full-stack applications, hooks build to package full-stack applications, and on the server side, you can also use hooks start to start the application with one click.

Solve your worries when using full-stack applications.

Powerful backend

Midway Hooks is developed based on Midway.

Midway is an 8-year-old Node.js framework with powerful back-end functions, including Cache / Redis / Mongodb / Task / Config and other commonly used components under the Web.

And all of this you can enjoy seamlessly when using Midway Hooks.

create application

Midway Hooks currently provides the following templates:

The command to create an application based on the specification is as follows:

npx degit https://github.com/midwayjs/hooks/examples/<name>

The full stack application command to create a react template is as follows:

npx degit https://github.com/midwayjs/hooks/examples/react ./hooks-app

The application command to create an api template is as follows:

npx degit https://github.com/midwayjs/hooks/examples/api ./hooks-app

Next step

  • Learn how to develop interfaces and provide them for front-end calls: Interface Development
  • How to use and create reusable Hooks: Hooks
  • How to validate user parameters at runtime: validate