【问题标题】:Google App Engile , Node JS, not work serving javascript static filesGoogle App Engine,Node JS,无法提供 javascript 静态文件
【发布时间】:2019-11-01 11:04:54
【问题描述】:

当我将 css 放入地址栏中时,图像可以工作,但我的 javascript 文件不能工作。

例如:

https://(...).appspot.com/build/teste.css(工作) https://(...).appspot.com/build/teste.js(404 错误 - 在此服务器上找不到请求的 URL /build/mycomponent.js。)

我的 app.yaml:


runtime: nodejs10


handlers:
- url: /images
  static_dir: static/images
  expiration: '10s'

- url: /css
  static_dir: static/css
  expiration: '10s'

- url: /build
  static_dir: static/build
  expiration: '10s'

- url: /assets
  static_dir: static/assets
  expiration: '10s'

- url: /static
  static_dir: static/.*
  expiration: '10s'

- url: /.*
  secure: always
  redirect_http_response_code: 301
  script: auto

我的结构文件夹是:

/static
   /css 
      / *.css(work)
   /images 
      / *.png|jpg (work)
   /assets
      / *.js (not work - 404 in log view)
   /build
      / test.css (work)
      / test.js (not work - 404 in log view) 

我的 index.ts 文件是:



import * as express from "express";
import * as fs from "fs";
import * as ejs from "ejs";


const VERSION : string = "1.0.6"; 
const PORT = Number(process.env.PORT) || 8080;
const app = express();



app.use(express.static('static'));
app.set("view engine", "html");
app.engine("html", ejs.renderFile);
app.set("views", __dirname + "/views");


app.use(function(req,res){
    res.render("index");
});



app.listen(PORT, () => {
    console.log(`Server version ${VERSION}`);
    console.log(`App listening on port ${PORT}`);
});

PS:satic_files 也有同样的问题。我试过了:


- url: /build/(.+)
  static_files: static/build/\1
  upload: static/build/(.*)

- url: /build/.*
  static_dir: static/build

【问题讨论】:

  • 嗨,你可以看看这个previous question。它讨论了处理程序,并且有一个带有 javascript 内容的示例。 ` url: /(.*\.js) mime_type: text/javascript static_files: static/\1 上传: static/(.*\.js)`
  • 使用 static_files 或 static_dir 的问题是一样的,只接受非 javascrupt 的文件。我试过 - url: /build/(.*) static_files: static/build/\1 上传: static/build/(.*) - url: /build/.* static_dir: static/build
  • 使用 url: /build/(.+) 也不行
  • 你试过不使用 /build 吗?只是 https://(...).appspot.com/teste.js 我已经执行了一个示例,我有以下结构: -app.yaml -static/ ----example.js 我已经设置了这个处理程序在 yaml 文件中: - url: /(.*\.js) mime_type: text/javascript static_files: static/\1 upload: static/(.*\.js) 当我访问 js 资源时,我会这样做:https:/ /(...).appspot.com/example.js 并且它有效。如果我尝试使用 /static/example.js 这不起作用。

标签: node.js typescript google-app-engine


【解决方案1】:

您是否尝试过不使用 /build 的请求?只是https://(...).appspot.com/teste.js

我做了一个例子,我的结构如下:

-app.yaml -static/ ----example.js

我已经在 yaml 文件中设置了这个处理程序:

- url: /(.*\.js) mime_type: text/javascript static_files: static/\1 upload: static/(.*\.js)

当我访问 js 资源时,我会这样做:https://(...).appspot.com/example.js,它可以工作。如果我尝试使用 /static/example.js 这不起作用。

我尝试过与您类似的结构:

-app.yaml -static/ ----assets/ --------example.js

使用此处理程序:

handlers: - url: /static static_dir: static/.* expiration: '10s' - url: /(.*\.js) mime_type: text/javascript static_files: static/assets/\1 upload: static/(.*\.js)

尝试过:https://(...).appspot.com/example.js > 有效 试过了:https://(...).appspot.com/assets/example.js > 不工作

我已关注this github

【讨论】:

  • 在你的例子中对我不起作用,因为没有 catch 301 错误。
猜你喜欢
  • 1970-01-01
  • 2017-08-15
  • 2019-05-19
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
相关资源
最近更新 更多