当前位置:网站首页>Nestjs configures static resources, template engine, and post examples
Nestjs configures static resources, template engine, and post examples
2022-06-30 05:06:00 【Johnny, me】
Configuring static resources
- Reference documents :https://docs.nestjs.com/techniques/mvc
The above is the official configuration
// The official sample :main.ts
import {
NestFactory } from '@nestjs/core';
import {
NestExpressApplication } from '@nestjs/platform-express'; // Here is http platform
import {
join } from 'path';
import {
AppModule } from './app.module';
async function bootstrap() {
// there create Method is a generic type , Incoming to us http platform
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
app.useStaticAssets(join(__dirname, '..', 'public')); // In this way, the static resource directory can be configured This line of code is the most critical
app.setBaseViewsDir(join(__dirname, '..', 'views')); // This is the configuration based on the template file stored in the template engine
app.setViewEngine('hbs'); // This is to set the template engine , We can choose what we are familiar with ejs
await app.listen(3000);
}
bootstrap();
This is the simplest configuration
// The official sample :main.ts
import {
NestFactory } from '@nestjs/core';
import {
NestExpressApplication } from '@nestjs/platform-express'; // Here is http platform
import {
AppModule } from './app.module';
async function bootstrap() {
// there create Method is a generic type , Incoming to us http platform
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
app.useStaticAssets('public'); // In this way, the static resource directory can be configured
await app.listen(3000);
}
bootstrap();
Set virtual path
// The official sample :main.ts
import {
NestFactory } from '@nestjs/core';
import {
NestExpressApplication } from '@nestjs/platform-express'; // Here is http platform
import {
AppModule } from './app.module';
async function bootstrap() {
// there create Method is a generic type , Incoming to us http platform
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
// The virtual path is static
app.useStaticAssets('public', {
prefix: 'static/'
});
// This is the configuration based on the template file stored in the template engine
app.setBaseViewsDir('views');
// This is to set the template engine , We can choose what we are familiar with ejs You need to install :yarn add ejs
app.setViewEngine('ejs');
await app.listen(3000);
}
bootstrap();
- Distinguish the template files of the front and background management systems , Generally we will be in views Partition in directory
- Example :
- views/
- default This is where the foreground template files are stored
- index.ejs
- news.ejs
- about.ejs
- admin The background template file is stored here
- index.ejs
- news.ejs
- goods.ejs
- default This is where the foreground template files are stored
- views/
In the controller based on ejs To render a page
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};
}
}
Write a simple template engine
// default/index.ejs
<div> full name : <%=name%></div>
<div> Age : <%=age%></div>
Test how forms are submitted , A simple little demo
Write the shopping cart controller
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'); } }Write add shopping cart components
<!-- Loading simulation of static resources --> <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=" Please enter the product name "> </div> <div> <input type="text" name="number" placeholder=" Please enter the quantity of goods "> </div> <input type="submit" value=" Submit " /> </form>
边栏推荐
- PS1 Contemporary Art Center, Museum of modern art, New York
- Unreal 4 learning notes - data storage using blueprints
- 炒美原油的国际交易平台如何能保障资金安全呢?
- Under what conditions does the Z-index attribute expire?
- Modbus protocol register
- Singapore must visit these scenic spots during the Spring Festival
- [vcs+verdi joint simulation] ~ take the counter as an example
- Spring Festival Tourism Strategy: welcome the new year in Bangkok, Thailand
- Meet in Bangkok for a romantic trip on Valentine's Day
- 产生 BUG 测试人员需要自己去分析原因吗?
猜你喜欢

Spring Festival Tourism Strategy: welcome the new year in Bangkok, Thailand

Oracle-数据的基本操作

Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)

PBR material: basic principle and simple fabrication

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

A must see cruise experience in Bangkok: visit the Mekong River and enjoy the scenery on both sides of the river

Basic operations of Oracle data

Li Kou 2049: count the number of nodes with the highest score

力扣349. 两个数组的交集

redis集群概念
随机推荐
Unity download and installation website
Force buckle 209 Minimum length subarray
Error about the new version of UE4: unavigationsystemv1:: simplemovetoactor has been deprecated
LxC and LXD container summary
A must see cruise experience in Bangkok: visit the Mekong River and enjoy the scenery on both sides of the river
PWN入门(2)栈溢出基础
Photon pun refresh hall room list
Unreal 4 learning notes - Animated Montage
Unity3d Google Earth
Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
Unity automatic pathfinding
MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
Create transfer generation point
Unity realizes rotation and Revolution
Nestjs入门和环境搭建
Sourcetree usage
Tcp/ip protocol details Volume I (Reading Guide)
Postman 做测试的 6 个常见问题
Untiy3d controls scene screenshots through external JSON files
Nestjs配置静态资源,模板引擎以及Post示例