Create the first application
Midway has multiple sets of technical solutions to choose from. We will distinguish them by deployment:
|Pure Node.js project
|Midway traditional project, pure Node.js research and development, modules represented by
@midwayjs/koa, supports back-end projects in the most complete way, and uses dependency injection + Class as the technology stack.
|Midway is a technology stack developed separately for Serverless scenarios. Modules represented by
@midwayjs/faas are connected to different Serverless platforms in a lightweight way.
|Midway's innovative technology scheme adopts the integrated development method of front and back ends to save the time of front and back ends. For modules represented by
@midwayjs/hooks, functional is used as the main coding paradigm.
npm init midway to view the complete list of scaffolds. After a project is selected, Midway automatically creates sample directories, codes, and installation dependencies.
$ npm init midway@latest -y
For a v3 project, select
koa-v3, pay attention to Node.js environment requirements.
The example will create a directory structure similar to the following, where the simplest Midway project example is as follows.
➜ my_midway_app tree
├── src ## midway project source code
│ └── controller ## Web Controller Directory
│ └── home.controller.ts
The whole project includes some of the most basic files and directories.
srcis the source directory of the entire Midway project.
- The test directory of the
testproject. All the test files are available here.
package thepackage management profile of the. json Node.js project Foundation
tsconfig.jsonCompile Configuration File TypeScript
In addition to the entire directory, we have some other directories, such as the
Midway has no special restrictions on directories, but we will follow some simple development habits and classify some commonly used files into some default folders.
The following ts source code folders are in the
Commonly used are:
controllerWeb Controller directory
serviceservice logical directory
modeldatabase entity directory
- The directory where the
utiltool class is stored.
decoratorcustom decorator directory
interface.tsdefinition file for ts business
With the emergence of different scenarios, directory habits will continue to increase, and the specific directory content will be reflected in different component functions.
Web framework selection
Midway was designed to be compatible with a variety of upper-level frameworks, such as common
Starting with v3, we use Koa to demonstrate the basic example.
These upper-level frameworks are provided in Midway with component capabilities, and all of them can use the decorator capabilities provided by Midway, but Midway will not encapsulate specific capabilities, such as the plugin system of Egg.js, or the middleware capabilities of Express, If you are familiar with one of these frameworks, or want to use the capabilities of a particular framework, you can choose it as your workhorse web framework.
|By default, Koa is a Express alternative framework, which supports asynchronous middleware and other capabilities by default, and is the second most common Node.js Web framework.
|Egg.js is a relatively commonly used Web framework in China, including some default plug-ins.
|Express is a well-known node.js minimalist Web framework. It's a well-tested, productive library with a lot of community resources.
Start the project
$ npm run dev
$ open http://localhost:7001
Midway will start the HTTP server, open the browser, access
http:// 127.0.0.1:7001, and the browser will print out the
Hello midwayjs! The information.
If you need to modify the development startup port, you can modify it in the scripts paragraph of the
package.json, such as 6001:
- Use mwtsc
- Use @midwayjs/cli
"dev": "cross-env NODE_ENV=local mwtsc --watch --run @midwayjs/mock/app.js --port 6001",
"dev": "cross-env NODE_ENV=local midway-bin dev --ts --port=6001",
Frequently Asked Questions
windows eslint error
Windows may encounter the problem of eslint error. Please pay attention to the problem of line wrapping under windows.