当前位置:网站首页>Nestjs配置静态资源,模板引擎以及Post示例
Nestjs配置静态资源,模板引擎以及Post示例
2022-06-30 05:01:00 【Johnny丶me】
配置静态资源
- 参考文档:https://docs.nestjs.com/techniques/mvc
上述是官方配置
// 官方示例:main.ts
import {
NestFactory } from '@nestjs/core';
import {
NestExpressApplication } from '@nestjs/platform-express'; // 这里是http平台
import {
join } from 'path';
import {
AppModule } from './app.module';
async function bootstrap() {
// 这里的create方法是一个泛型,传入我们的http平台
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
app.useStaticAssets(join(__dirname, '..', 'public')); // 这样即可配置好静态资源目录 这行代码最关键
app.setBaseViewsDir(join(__dirname, '..', 'views')); // 这是配置基于模板引擎存放的模板文件
app.setViewEngine('hbs'); // 这是设置模板引擎, 我们可以选择我们熟悉的ejs
await app.listen(3000);
}
bootstrap();
这样可做最简单的配置
// 官方示例:main.ts
import {
NestFactory } from '@nestjs/core';
import {
NestExpressApplication } from '@nestjs/platform-express'; // 这里是http平台
import {
AppModule } from './app.module';
async function bootstrap() {
// 这里的create方法是一个泛型,传入我们的http平台
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
app.useStaticAssets('public'); // 这样即可配置好静态资源目录
await app.listen(3000);
}
bootstrap();
设置虚拟路径
// 官方示例:main.ts
import {
NestFactory } from '@nestjs/core';
import {
NestExpressApplication } from '@nestjs/platform-express'; // 这里是http平台
import {
AppModule } from './app.module';
async function bootstrap() {
// 这里的create方法是一个泛型,传入我们的http平台
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
// 虚拟路径为 static
app.useStaticAssets('public', {
prefix: 'static/'
});
// 这是配置基于模板引擎存放的模板文件
app.setBaseViewsDir('views');
// 这是设置模板引擎, 我们可以选择我们熟悉的ejs 需要先安装:yarn add ejs
app.setViewEngine('ejs');
await app.listen(3000);
}
bootstrap();
- 区分前后台管理系统的模板文件,一般我们会在views目录中划分
- 示例:
- views/
- default 这里存放前台模板文件
- index.ejs
- news.ejs
- about.ejs
- admin 这里存放后台模板文件
- index.ejs
- news.ejs
- goods.ejs
- default 这里存放前台模板文件
- views/
在控制器中基于ejs来渲染页面
import {
Controller, Get, Render } from '@nest/common';
import {
AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {
}
@Get()
@Render('default/index')
index() {
return {
name: 'Joh', age: 10};
}
}
编写简单的模板引擎
// default/index.ejs
<div>姓名: <%=name%></div>
<div>年龄: <%=age%></div>
测试如何表单提交, 简单一个小demo
编写购物车控制器
import { Controller, Get, Post, Body, Response, Render } from '@nestjs/common'; @Controller('cart') export class CartController { @Get() @Render('default/cart') index() { // do nothing } @Post('add') add(@Body() body, @Response() res) { console.log(body); res.redirect('/cart'); } }编写添加购物车组件
<!-- 静态资源的加载模拟 --> <link rel="stylesheet" href="/static/apple.css"> <img src="/static/apple.png" /> <form action="/cart/add" method="post"> <div> <input type="text" name="name" placeholder="请输入商品名称"> </div> <div> <input type="text" name="number" placeholder="请输入商品数量"> </div> <input type="submit" value="提交" /> </form>
边栏推荐
- Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
- Connect to the database and run node JS running database shows that the database is missing
- Malignant bug: 1252 of unit MySQL export
- National Museum of Singapore - give you spiritual and physical satisfaction
- Modbus protocol register
- 【Paper】2019_ Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D
- Easyrecovery data recovery software recovers my photo and video data two years ago
- Webots learning notes
- Passing values between classes using delegates and events
- 【Paper】2017_ Distributed control for high-speed trains movements
猜你喜欢

【Paper】2019_ Distributed Cooperative Control of a High-speed Train

National Museum of Singapore - give you spiritual and physical satisfaction

PS1 Contemporary Art Center, Museum of modern art, New York

Unity lens making

Some problems encountered in unity steamvr

Royal Albert Hall, a popular landmark in London

Procedural animation -- inverse kinematics of tentacles

Exploration of unity webgl

Some books you should not miss when you are new to the workplace

【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
随机推荐
Unreal 4 learning notes - data storage using blueprints
【VCS+Verdi联合仿真】~ 以计数器为例
The difference between SVG and canvas
[control] multi agent system summary. 4. control agreement.
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
Royal Albert Hall, a popular landmark in London
力扣349. 两个数组的交集
2021-03-16
Unrealeengine4 - about uobject's giant pit that is automatically GC garbage collected
Important knowledge points in unity3d
harbor api 2.0查询
Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
Ripple effect of mouse click (unity & shader)
【Paper】2019_ Distributed Cooperative Control of a High-speed Train
How to install win7 on AMD Ruilong CPU A320 series motherboard
redis集群概念
Malignant bug: 1252 of unit MySQL export
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example
Redis cluster concept
Force buckle 59 Spiral matrix II