小米商城全栈项目

简介

在本篇博客中,我们将一步步地创建一个基于NestJS的电商后端服务。我们将从项目初始化开始,逐步完成基本的后端服务搭建,包括数据库导入、接口开发,以及进阶的登录鉴权和动态数据渲染。这将是一个全面的实战教程,适合想要深入了解NestJS和后端服务开发的开发者。

环境准备

在开始之前,请确保你已经安装了以下工具:

  • Node.js

  • npm 或 yarn

  • MySQL

  • Visual Studio Code

步骤1:创建NestJS项目

首先,我们需要创建一个新的NestJS项目。打开终端,运行以下命令:

bash

复制代码
nest new ecommerce-backend

这将创建一个新的NestJS项目,并安装所有必要的依赖。

步骤2:搭建后端服务

进入项目目录,开始搭建后端服务。

bash

复制代码
cd ecommerce-backend

导入SQL数据

在这一步,我们需要将预先准备好的SQL数据导入到数据库中。确保你的数据库服务正在运行,然后使用数据库管理工具(如MySQL Workbench或pgAdmin)导入数据。

步骤3:开发接口

我们将开发几个关键的API接口,包括首页轮播图、我的页面、首页动态数据渲染等。

首页轮播图接口

创建一个Carousel模块,包含CarouselControllerCarouselService。在CarouselController中,定义一个获取轮播图数据的方法。

typescript

复制代码
// swiper.controller.ts
@Controller('swiper')
export class SwiperController {
  constructor(private readonly swiperService: SwiperService) {}
​
  @Get()
  async getSwipers(): Promise<Swiper[]> {
    return this.swiperService.findAll();
  }
}
​

首页数据接口

typescript

复制代码
​
@Controller('/index')
export class IndexController {
  constructor(private readonly indexService: IndexService) {}
​
  @Get()
  async findAll(): Promise<Goods[]> {
    return this.indexService.findAll();
  }
}

我的接口

UserController中,添加一个获取用户信息的方法。

typescript

复制代码
// user.controller.ts
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}
​
  @Get()
  getUserInfo(): User {
    return this.userService.getUserInfo();
  }
}

步骤4:动态数据渲染

对于首页和我的页面,我们需要实现动态数据渲染。这通常涉及到从数据库中查询数据,并将其返回给前端。

首页动态数据渲染

HomePageController中,添加一个方法来获取首页数据。

typescript

复制代码
// home-page.controller.ts
@Controller('home-page')
export class HomePageController {
  constructor(private readonly homePageService: HomePageService) {}
​
  @Get()
  getHomePageData(): HomePageData {
    return this.homePageService.getHomePageData();
  }
}

步骤5:静态页面渲染

对于分类页面、米圈页面和购物车页面,我们将实现静态页面渲染。

分类页面静态渲染

CategoryController中,添加一个方法来返回分类页面的静态数据。

typescript

复制代码
// category.controller.ts
@Controller('category')
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}
​
  @Get()
  getCategoryData(): Category[] {
    return this.categoryService.getCategories();
  }
}

进阶要求

登录接口鉴权

使用NestJS的Guards来实现登录接口鉴权。

typescript

复制代码
// auth.guard.ts
@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    const request = context.switchToHttp().getRequest();
    const token = request.headers['authorization'];
    // 验证token逻辑...
    return true;
  }
}

动态数据渲染

对于分类页面和米圈页面,实现动态数据渲染。

购物车商品数据渲染

CartController中,添加一个方法来获取购物车商品数据。

typescript

复制代码
// cart.controller.ts
@Controller('cart')
export class CartController {
  constructor(private readonly cartService: CartService) {}
​
  @Get()
  getCartData(): CartItem[] {
    return this.cartService.getCartItems();
  }
}

总结

通过本次项目,我从零开始,一步步地构建了一个基于NestJS的电商后端服务。我们完成了项目创建、数据库导入、接口开发、静态和动态数据渲染,以及登录鉴权。希望这个实战教程能帮助你更好地理解和使用NestJS。

相关推荐
Penge6666 小时前
Go 接口编译期断言
后端
我是一颗柠檬6 小时前
【MySQL全面教学】MySQL面试高频考点汇总Day15(2026年)
数据库·后端·mysql·面试
拽着尾巴的鱼儿6 小时前
springboot openfeign 自定义feign 接口重试机制
java·spring boot·后端
Ceelog7 小时前
久坐党自救指南:屏幕前 8 小时,身体到底在经历什么
前端·后端
身如柳絮随风扬7 小时前
数据库读写分离:从原理到实战,构建高并发系统
数据库·mysql
swipe7 小时前
DeepAgents 实战:用多 Agent 架构搭一个深度调研助手
javascript·面试·llm
XS0301068 小时前
并发编程 六
java·后端
雪宫街道8 小时前
synchronized 锁的范围:对象锁、类锁与代码块锁
java·jvm·后端·面试
云水一下9 小时前
JavaScript 从零基础到精通系列:前世今生与编程启蒙
前端·javascript