nest学习1:项目搭建

文章问题导向

nest如何创建项目?
nest如何使用快捷命令生成文件?

如果你都有了答案,可以忽略本文章,或去nest学习导图寻找更多答案


如何创建项目?
第一步:全局安装nestjs脚手架,用于搭建项目

npm install -g @nestjs/cli
或者
yarn global add @nestjs/cli

安装完毕后,验证是否成功:查看版本号,显示版本号则安装成功

nest -v


第二步:创建项目
cd到你的安装目录,输入命令选择安装的方式,gitbash可以使用数字1,2来选择安装方式

nest new ProjectName


第三步:安装依赖项

yarn 或者 npm install

项目基本目录

src
	app.controller.spec.ts   //测试文件
	app.module.ts            //根模块,引入app.controller.ts 和 app.service.ts,用于被入口文件使用
	app.controller.ts        //控制器,用于创建接口,编写逻辑,引入app.service.ts
	app.service.ts           //服务,用于查询数据库
	main.ts                  //入口文件,启动项目,全局配置,引入app.module.ts

第四步:启动项目
nest项目默认在3000端口启动,可以在src → main.ts中修改 app.listen(3000)

yarn start 或者 npm start

在浏览器打开localhost:3000 或 http://127.0.0.1:3000/ 即可看见传说中的 hello world


如何使用快捷命令生成文件?

主要命令:nest g --help,可以查看所有的快捷命令
常用命令:
创建控制器:nest g co user module
创建服务:nest g s user module
创建模块:nest g mo user module
nest g(固定写法) co(生成的内容) user(文件名) module(路径,默认以src为根路径)


学习更多

nest学习导图

你可能感兴趣的:(nest,node.js)