preface
As I mentioned in my previous post on Midway, I didn’t have a lot of projects to write about, so I took a page out of the CNode community and rebuilt it with Midway by using the egg.js forum. Refactoring wasn’t too difficult because Midway itself was based on egg.js. Most of the code can be used directly.
Project initialization
First, use the Midway init tool to create a directory structure for your Midway application
$NPM install midway-init -g $midway-init // You can use --type to specify the template name $midway-init --type midway-ts-ant-design-proCopy the code
The created Midway directory is as follows
. ├ ─ ─ the README. Md ├ ─ ─ the README. Useful - CN. Md ├ ─ ─ dist directory - compiled ├ ─ ─ logs - local log directory │ └ ─ ─ midway6 - test - log application name beginning │ ├ ─ ─ Common-error. log ---- error log │ ├── Midway Agent. log ---- Agent's log │ ├── midway Core ├─ ├─ download.txt ---- │ ├─ ├─ download.txt ├─ download.txt ├─ download.txt ├─ download.txt ├── ├─ ---- Web Layer Controller directory │ │ ├─ home.ts │ │ ├── middleware (optional) ---- Web layer middleware directory │ │ ├─ ├─ ---- Web layer Controller directory │ │ ├─ home │ └ ─ ─ trace. Ts │ │ ├ ─ ─ public static file directory (optional) - the web tier, Can configure │ │ ├ ─ ─ the view (optional) │ │ | └ ─ ─ home. TPL - web layer template │ ├ ─ ─ the config │ │ ├ ─ ─ config. The default. The ts │ │ ├ ─ ─ config. Local. Ts │ │ ├ ─ ─ config. Prod. Ts │ │ ├ ─ ─ config. The unittest. Ts │ │ └ ─ ─ the plugin. The ts │ └ ─ ─ the lib directory, business logic layer, │ ├─ ├─ ---- │ ├─ ├─ user.ts │ ├─ user.ts ---- │ ├─ app.ts ---- │ ├─ ├─ ---- ├─test│ └ ─ ─ app │ └ ─ ─ controller │ └ ─ ─ home. Test. The ts ├ ─ ─ tsconfig. Json └ ─ ─ tslint. JsonCopy the code
We first install the dependencies through commands, and then drive the commands through NPM scripts
$ npm install
$ npm run dev
Copy the code
When NPM run dev is started, the terminal will output some information. If the following information is displayed and the program does not exit abnormally, the startup is successful
2019-07-25 13:47:17,662 INFO 45506 [Master] Midway Started on http://127.0.0.1:7001 (4523ms 127.0.0.1:7001 Welcome to midwayjs!Copy the code
We moved the CNode front-end resources and pages to our Midway directory. Copy all the files in the public directory of the CNode project directory APP to the SRC/APP /public directory of the project we just created.
$ cd /src/app/public
$ ls
README.md images javascripts stylesheets
github-card.html img libs upload
Copy the code
Then copy all files in the CNode Views directory to the/SRC /app/view directory of our newly created project. If this directory does not exist, please create a new one.
$ cd/src/app/view $ ls _ads.html index.html reply static _sponsors.html layout.html search topic editor_sidebar.html message sidebar.html user includes notify signCopy the code
CNode uses Redis and Mongodb, so let’s install the redis and Mongodb plug-ins.
Mongoose NPM install egg-mongoose --save NPM install egg-redis --saveCopy the code