[英]How can I serve static images with spaces in the filename using Meteor?
[英]How to serve static images in NestJS
我开始学习 MEAN 堆栈,当我去 Express 时,我看到 express 框架中存在一个额外的层,称为 NestJS。 它拥有我想要的一切,并且具有类似 Angular 的语法,因此对我来说非常完美。
但是每一个新步骤都是一场噩梦,文档一点用都没有。 现在我正在使用框架来实现图像服务,并且不使用 API 进行此类调用。
我尝试了我在互联网上找到的所有内容,例如:
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as bodyParser from 'body-parser';
import * as express from 'express';
import { join } from 'path';
import { NestExpressApplication } from '@nestjs/platform-express';
declare const module: any;
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useStaticAssets(join(__dirname, '..', 'public'));
app.enableCors({
origin: true,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true,
});
//I tried this 2 options (https://docs.nestjs.com/techniques/mvc) (https://whatthecode.dev/serve-static-files-with-nest-js/)
app.use('/resources', express.static(process.cwd() + '\\resources'));
app.useStaticAssets(join(__dirname, '..', '\\public'));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
//console.log(process.cwd());
await app.listen(3000);
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => app.close());
}
}
bootstrap();
我试图把它放在 app.module 中(它有效,但总是在寻找索引。html 不是图像):
import { AnimalsModule } from './animals/animals.module';
import { SpeciesModule } from './species/species.module';
import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { BreedModule } from './breed/breed.module';
import { StateModule } from './state/state.module';
import { PhotoModule } from './photo/photo.module';
@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'public'), // <-- path to the static files
}),
TypeOrmModule.forRoot({
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: '',
database: 'nest',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
AnimalsModule,
SpeciesModule,
BreedModule,
StateModule,
AuthModule,
UsersModule,
PhotoModule,
],
})
//It seems that ignores this register and just uses the method signature options
@Module({
imports: [MulterModule.register({
dest: './resources/tmp',
limits: { fieldSize: 25 * 1024 * 1024 * 1024, fileSize: 25 * 1024 * 1024 * 1024 }
})],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
我怎么能提供图像或文件并避免 api 路由?
谢谢大家。
这就是我所做的:
在 app.module.ts
import { ServeStaticModule } from '@nestjs/serve-static/dist/serve-static.module'; import { join } from 'path'; @Module({ imports: [ ServeStaticModule.forRoot({ rootPath: join(__dirname, '..', 'public'), }), ], })
然后我在与“src”、“dir”等相同的级别上创建了“public”目录。
该文件位于:https://my-url/file.jpg
使用useStaticAssets()
方法( ServeStaticModule
用于提供 static 内容,如 SPA)。 因此,作为一个最小的示例,您的main.ts
应如下所示):
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
app.useStaticAssets(join(__dirname, '..', 'public'));
await app.listen(3000);
}
在上面的示例中,您的资产是从应用程序的根目录提供的(例如http://localhost:3000/example.png
。
您还可以将选项传递给该方法,以配置提供资产的路径:
app.useStaticAssets(join(__dirname, '..', 'public'), {
prefix: '/public/',
});
在此示例中,您的路径以public
为前缀(例如http://localhost:3000/public/example.png
)
有两种方法可以在NestJs
中提供 static 内容。 要么使用1
OR 2
。
1.在ServeStaticModule
中使用ServeStaticModule
import { Module } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { mainModule } from 'process'
@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'public'),
}),
],
})
export class AppModule {}
2.在NestExpressApplication
中使用NestExpressApplication 【即使默认Nest Js也只使用Express】
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
app.useStaticAssets(join(__dirname, '..', 'public'));
await app.listen(3000);
}
bootstrap();
将此添加到 main.ts 将“uploads”替换为您的目录名称 app.use('/uploads',express.static(join(process.cwd(), 'uploads')));
@pzabrocki 的想法将我带到了以下最小的工作存储库: https://gitlab.com/hadrien-toma/nest-static-files
玩它:
git clone https://gitlab.com/hadrien-toma/nest-static-files
yarn install
yarn run start
然后my-file.json
在http://localhost:3333/my-file.json
。
分离模块解决方案,然后将其导入 AppModule。
import { Module } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { resolve } from 'path';
@Module({
imports: [
ServeStaticModule.forRoot(
(() => {
const publicDir = resolve('./public/files/');
const servePath = '/files';
return {
rootPath: publicDir,
// serveRoot - if you want to see files on another controller,
// e.g.: http://localhost:8088/files/1.png
serveRoot: servePath,
exclude: ['/api*'],
};
})()
),
],
})
export class PublicModule {}
使用该模块进行文档嵌套的主要方法如下:
ServeStaticModule.forRoot({
rootPath: join(__dirname, '..', 'upload'), // path to the static files image
}),
但是如果你在搜索index.html
时遇到错误,你可以使用这个方法,我也用过
main.ts
import * as express from 'express';
app.use(express.static(join(__dirname, '../upload')))
您也可以在嵌套 js 中使用 express static 模块。
// ...imports
import * as express from 'express';
import { join } from 'path';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// serve static assets from the client/dist folder, change this to the correct path for your project
app.use(express.static(join(process.cwd(), '../client/dist/')));
//... you config
await app.listen(process.env.PORT);
}
bootstrap();
归功于:https://whatthecode.dev/serve-static-files-with-nest-js/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.