Install and build the development environment for the project

IKcamp Production team 🇨🇳

Original author: Dahum, Gan, SAN SAN, Tiger, Fatty, Ha, DDU, Kwood, Wobble Copy proofreading: Li Yi, Da Li Meng, Au, DDU, Brook, Ha Style Anchor: Kwood, Gan, Au, DDU, Ha Video clip: Brook main station operation: Geili XI, XTY tutorial editor: Zhang Li Tao

Video address: www.cctalk.com/v/151143577…

The article

Koa start – Environment preparation

Since KOA2 has started using new syntax such as async/await, make sure the Node environment is above version 7.6.


Installation node. Js


  • Install node.js directly: node.js official address nodejs.org
  • NVM manages multiple versions of Node. js: You can use NVM to manage node versions
    • Install NVM for Mac system github.com/creationix/…
    • Install NVM for Windows github.com/coreybutler…
    • Install NVM for Ubuntu github.com/creationix/…


Project initialization

My first move as a programmer: “Hello World”


First, create a directory, koA2-tutorial /, to hold our code. Then start initializing the project:

// Create package.json file. This file is used to manage the installation packages used in the project
npm init
Copy the code

After the project is initialized, create a new file app.js in the directory you created and write:

console.log('Hello World')
Copy the code

Now, our project structure should look like this:

├ ─ ─ app. Js ├ ─ ─ package. The jsonCopy the code

Open the console, go to the directory koa2-tutorial/ and type:

node app.js
Copy the code

If Hello World is displayed successfully, the environment is normal. At this point, our preparatory work is complete.

Next we will start the server based on Koa2.


Start the server

Run the following command to install Koa (version information is automatically saved in package.json)

// Install KOA and save the version information in package.json
npm i koa -S
Copy the code

Rewrite app.js and add the following code:

const Koa = require('koa')
const app = new Koa()

app.listen(3000, () = > {console.log('server is running at http://localhost:3000')})Copy the code

Run Node app.js and open a browser to access localhost:3000. The “Not Found” page is displayed.

Because the code does nothing else after starting the server, there is no interaction.

We continue to modify the app.js file:

const Koa = require('koa')
const app = new Koa()

// Add code
app.use(async (ctx, next) => {
  await next()
  ctx.response.type = 'text/html'
  ctx.response.body = '<h1>Hello World</h1>'
})

app.listen(3000, () = > {console.log('server is running at http://localhost:3000')})Copy the code

Restart the server and visit again, and the page will normally display Hello World.


In the added code, Koa’s “middleware” is used, so what is “middleware”? We’ll talk more about that in the next video.

Next: Middleware Usage – Explain the usage of Koa2 Middleware and how to develop middleware (video included)

The previous: iKcamp to launch new curriculum ~ ~ ~ ~ ~ start free serial ~ 2 more per week, a total of 11 iKcamp lesson | based on Koa2 structures, the Node. Js combat (video) | project teaching syllabus is introduced


In 2019, iKcamp’s original new book Koa and Node.js Development Actual Combat has been sold on JD.com, Tmall, Amazon and Dangdang!