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

时间:2017-05-04 22:09:47

标签: node.js angular angular2-routing

我必须按照以下步骤验证我的应用,但是我无法建立链接到我的文件的路径。

http://imgur.com/a/X2ZXU

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

2 个答案:

答案 0 :(得分:3)

您根本不需要在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

正如您所看到的,这是将Web服务器配置为提供静态文件的问题 - 默认情况下应该是最常见的。

答案 1 :(得分:0)

您可能已经找到了一种方法来执行此操作,但是这是我能够通过暴动验证我的应用程序的方式:

在我的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;
    }
}

我正在使用使用此Dockerfile构建的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;"]

希望您觉得这有用。