【问题标题】:404 Not Found for index.html in Google App Engine - How to get page to show up?404 Not Found for index.html in Google App Engine - 如何让页面显示?
【发布时间】:2021-11-29 08:51:13
【问题描述】:

我正在尝试使用 Google App Engine 托管我的项目,但无法显示我的页面。我的 package.json 看起来像这样

  "name": "final",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "scripts": {
    "start": "node app.js"
}

我的 app.js 看起来像这样:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.sendFile('/index.html');
});

// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});

当我部署时,页面只是显示 Not Found 并给我一个 404。我需要对我的 index.html 和它使用的后续 .js 文件做些什么,或者我在部署时是否上传了整个目录?错误日志除了 404 之外什么都没有。

【问题讨论】:

  • 能否请您也添加app.yaml 文件?

标签: javascript node.js express google-app-engine


【解决方案1】:

需要指定index.html的完整路径

const express = require('express');
const app = express();
const path = require('path');

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, '/index.html'));
});

// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-04
    • 2018-12-03
    • 2013-11-09
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    相关资源
    最近更新 更多