官方文档:docs.nestjs.com/techniques/…javascript
app.useStaticAssets('public');
复制代码
完整代码:html
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useStaticAssets('public');
await app.listen(3000);
}
bootstrap();
复制代码
在根目录新建public
目录,而后在目录里面保存一个图片好比1.jpg
,这样就能够经过http://localhost:3000/1.jpg 来访问图片。 咱们也能够配置虚拟目录,好比咱们想经过http://localhost:3000/static/1.jpg
来访问public
目录里面的文件,这时候代码以下:java
app.useStaticAssets(join(__dirname, '..', 'public'),{
prefix: '/static/', //设置虚拟路径
});
复制代码
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import {join} from 'path';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
// app.useStaticAssets('public');
app.useStaticAssets(join(__dirname, '..', 'public'),{
prefix: '/static/', //设置虚拟路径
});
await app.listen(3000);
}
bootstrap();
复制代码
官方文档:docs.nestjs.com/techniques/… 1 、安装对应的模板引擎,好比ejs
cnpm i ejs --save
2 、 配置模板引擎express
app.setBaseViewsDir(join(__dirname, '..', 'views')) // 放视图的文件
app.setViewEngine('ejs');
复制代码
完整代码:npm
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
// app.useStaticAssets('public');
app.useStaticAssets(join(__dirname, '..', 'public'),{
prefix: '/static/', //设置虚拟路径
});
app.setBaseViewsDir(join(__dirname, '..', 'views')) // 放视图的文件
app.setViewEngine('ejs');
await app.listen(3000);
}
bootstrap();
复制代码
3 、渲染页面bootstrap
import { Get, Controller, Render } from '@nestjs/common';
@Controller()
export class AppController {
@Get()
@Render('index')
root() {
return { message: 'Hello world!' };
}
}
复制代码
4 、 项目根目录新建views,目录而后新建index.ejsmarkdown
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
这是 ejs 演示代码
<br>
<%=message%>
</body>
</html>
复制代码
import { Controller, Get, Post, Body,Response, Render} from '@nestjs/common';
@Controller('user')
export class UserController {
@Get()
@Render('default/user')
index(){
return {"name":"张三"};
}
@Post('doAdd')
doAdd(@Body() body,@Response() res){
console.log(body);
res.redirect('/user'); //路由跳转
}
}
复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<img src="/static/1.png" alt="" />
<br>
<form action="/user/doAdd" method="post">
<input type="text" name="username" placeholder="请输入用户名" />
<br>
<br>
<input type="text" name="age" placeholder="年龄" />
<br>
<br>
<input type="submit" value="提交">
</form>
</body>
</html>
复制代码