当前位置:网站首页>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>
边栏推荐
- Unity packaging and publishing webgl error reason exception: failed building webgl player
- Chapter 11 advanced data management of OpenGL super classic (version 7)
- Unity enables simple music visualization
- Connect() and disconnect() of socket in C #
- Unity Logitech steering wheel access
- Postman 做测试的 6 个常见问题
- C # three ways to obtain web page content
- Untiy3d controls scene screenshots through external JSON files
- Unity camera control
- Pytorchcnn image recognition and classification model training framework
猜你喜欢
Unity script life cycle and execution sequence
Win10 vs2015 compiling curaengine
One command to run rancher
PWN入门(2)栈溢出基础
Unity + hololens2 performance test
Procedural animation -- inverse kinematics of tentacles
Li Kou 2049: count the number of nodes with the highest score
Pit of smoothstep node in shadergraph
What is multimodal interaction?
Force buckle 977 Square of ordered array
随机推荐
PWN入门(2)栈溢出基础
How to install win7 on AMD Ruilong CPU A320 series motherboard
ParticleSystem in the official Manual of unity_ Collision module
The golden deer, a scenic spot in London -- a sailing museum that tells vivid sailing stories
Solution to Autowired annotation warning
JPA复合主键使用
Win10 vs2015 compiling curaengine
Unity project hosting platform plasticscm (learn to use 2)
Draw on screen border in Commodore 64
Unity realizes rotation and Revolution
Unity profiler performance analysis
Unity a* road finding force planning
Tcp/ip protocol details Volume I (Reading Guide)
Unity is associated with vs. there is a compiler problem when opening
Unity Logitech steering wheel access
Untiy3d controls scene screenshots through external JSON files
Unity packaging and publishing webgl error reason exception: failed building webgl player
MinGW-w64下载文件失败the file has been downloaded incorrectly!
GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;
【VCS+Verdi聯合仿真】~ 以計數器為例