Google App Engile,Node JS,无法提供javascript静态文件

时间:2019-11-01 11:04:54

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

当我将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

1 个答案:

答案 0 :(得分:0)

您是否尝试了不带/ 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