1, the first step, through the scaffolding installation to create the project

  • Global installation scaffolding: NPM i-g@nestjs /cli

  • Create project architecture: Nest New project name

  • Run project (hot update) : NPM start:dev

Step 2: Create modularity (MVC)

  • Command to create a module

Nest g Service [file name][file directory] // SRC directory

Create Controller: Nest G Controller [file name][file directory] // SRC

Nest g Module [file name][file directory] // SRC

  • Module Contact Description

Controller: indicates a Controller that provides apis

Service: Defines data, such as adding, deleting, modifying, and querying a database

Module: Connects Controller and Service

  • Module specific example (judging whether the input account password is correct and obtaining user information as an example)

 user.service.ts

import { Injectable } from '@nestjs/common'; Interface user {// Create array type user name: string, password: string, age: string, classes: String,} @injectable () // Declare that it can be instantiated by injection, Export class UserService {/* * return */ getData(): user[] {return [{name: {name: 'zhang 2', password: '23456', age: '18', classes:' computer Science and Technology 1'}, {name: 'Zhang 2', password: '23456', age: '18', classes: 'Computer Science and Technology 2'}, {name: 'zhang ', password: '34567', age: '18', classes:' Computer Science and Technology 3'}, {name: 'Zhang ', password: '34567', age: '18', classes:' Computer Science and Technology 3'}, {name: 'Zhang ', password: }]} /* * Check whether userIsExist(name: string, password: string): userIsExist(name: string, password: string) string { let data: user[] = this.getData() let flag: Boolean = false data.forEach((item: User) => {if (name === item.name && password === item.password) {flag = true}}) return flag? } /* * get user information */ getUserData(name: string): user {let data: user[] = this.getData() let res: user data. user) => { if (name === item.name) { res = item } }) return res; }}Copy the code

user.controller.ts

import { Controller, Post, Get, Body, Query } from '@nestjs/common'; import { UserService } from './user.service'; @controller ('user') export class UserController {// Constructor (private readonly UserService: UserService) {} // define post request @post ('userIsExist') userIsExist(@body () Body: any) { return this.UserService.userIsExist(body.name, body.password); } / / define a get request @ the get (' getUserData) getUserData (@ Query () Query: any) {return this. UserService. GetUserData (Query. Name); }}Copy the code

user.module.ts

import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';

@Module({
  controllers: [UserController],
  providers: [UserService],
  exports: [UserService]
})
export class UserModule {}
Copy the code

app.module.ts

import { UserModule } from './logical/user/user.module';

@Module({
  imports: [UserModule],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}
Copy the code

main.ts

import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.setGlobalPrefix('nodeApi'); // global API prefix await app.listen(3000); } bootstrap();Copy the code

3, question

What other methods can we define an array than any?

Check ts definition of the data, can use interface to solve

interface user { name: string; password: string; } array: let list: user[] object: let list: userCopy the code

4. Attach front-end VUE request code (using AXIOS request)

<template> <div id="app"> {{ myData }} </div> </template> <script> export default { data () { return { myData: '' } }, created () { this.getData() }, methods: { getData () { this.axios.post('/nodeApi/user/userIsExist', { name: '1', password: '12345'}), then ((res) = > {alert (res) data)}) enclosing axios. Get ('/nodeApi/user/getUserData ', {params: {name: 'picture 1'}}), then ((res) = > {this. MyData = res. Data})}}} < / script > < style lang = "less" > < / style >Copy the code

Running results: