如何在 Angular 应用程序中提供 txt 文件

How to serve a txt file in an Angular app

我必须按照这些步骤来验证我的应用程序,但我无法将 link 路由到我的文件。

http://imgur.com/a/X2ZXU

上面的 link 是我得到的说明,我创建了文本文件并将其放在 angular 2 应用程序的资产文件夹中。但是我不知道如何创建路由 http://www.example.com/riot.txt 以便它引用我的 txt 文档文件。谁能指出我正确的方向?我正在使用 Angular 2.

您根本不需要在 Angular 中创建路线。如果您使用的是 Angular CLI(或类似设置),您可以将文件放在 src 目录中并将其添加到 assets 属性 中的 .angular-cli.json 文件.

npm install -g @angular/cli
ng new blog
echo 'Hello, World' > src/greeting.txt

你的 .angular-cli.json 看起来像这样。

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "blog"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "greeting.txt",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}

然后你就可以运行 ng serve了。

ng serve

如您所见,这是配置您的 Web 服务器以提供静态文件的问题 - 默认情况下大多数应该如此。

您可能已经找到了执行此操作的方法,但这是我能够使用 riot 验证我的应用程序的方法:

在我的 Nginx 配置文件中,我添加了带有文件路径的 /location

server {
    listen 80;

    // this is what you use to handle your routes
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    // this is how I was able to verify my app
    location /riot.txt {
        root /usr/share/nginx/html;
        index index.html index.htm;
        try_files $uri $uri/ /assets/riot.txt;
    }
}

我正在使用 docker 镜像在 GKE 上部署此应用程序:

#############
### build ###
#############

# base image
FROM node:15.3.0-alpine3.10 as build

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY ./web/package.json /app/package.json
RUN npm install
RUN npm install -g @angular/cli

# add app
COPY ./web /app

# generate build
RUN ng build --output-path=dist

############
### prod ###
############

# base image
FROM nginx:1.16.0-alpine

# copy artifact build from the 'build environment'
COPY --from=build /app/dist /usr/share/nginx/html

# replace nginx default.conf with my conf (default.conf) 
COPY --from=build /app/default.conf /etc/nginx/conf.d/default.conf

# expose port 80
EXPOSE 80

# run nginx
CMD ["nginx", "-g", "daemon off;"]

希望你觉得这有用。