簡體   English   中英

在 Heroku 上部署 Node.js 應用程序時出現 403 錯誤

[英]403 error when deploying a Node.js app on Heroku

早些時候,我在嘗試使用 Heroku 打開 Node.js 應用程序時收到以下錯誤(來自 Chrome 控制台):

Refused to load the image 'https://browser-rpg-app.herokuapp.com/favicon.ico' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.

這伴隨着 403。我設法通過添加以下行來修復它:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js 'unsafe-inline'">

現在第一個錯誤消失了,但我仍然收到 403。我可以在heroku local web上完美地運行應用程序,但在我實際部署時卻不能。 這是日志所說的:

2019-12-02T22:41:29.000000+00:00 app[api]: Build succeeded
2019-12-02T22:41:32.617542+00:00 heroku[web.1]: Starting process with command `node app.js`
2019-12-02T22:41:36.786903+00:00 heroku[web.1]: State changed from starting to up
2019-12-02T22:42:00.484013+00:00 app[web.1]: ForbiddenError: Forbidden
2019-12-02T22:42:00.484062+00:00 app[web.1]: at SendStream.error (/app/node_modules/send/index.js:270:31)
2019-12-02T22:42:00.484066+00:00 app[web.1]: at SendStream.pipe (/app/node_modules/send/index.js:553:12)
2019-12-02T22:42:00.484068+00:00 app[web.1]: at sendfile (/app/node_modules/express/lib/response.js:1103:8)
2019-12-02T22:42:00.484071+00:00 app[web.1]: at ServerResponse.sendFile (/app/node_modules/express/lib/response.js:433:3)
2019-12-02T22:42:00.484074+00:00 app[web.1]: at index (/app/routes/index.js:9:9)
2019-12-02T22:42:00.484077+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2019-12-02T22:42:00.484079+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)
2019-12-02T22:42:00.484081+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2019-12-02T22:42:00.484083+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2019-12-02T22:42:00.484085+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22
2019-12-02T22:42:00.482239+00:00 heroku[router]: at=info method=GET path="/" host=browser-rpg-app.herokuapp.com request_id=845c8d30-4ca7-44f4-ab69-ae312e722b1b fwd="68.174.27.246" dyno=web.1 connect=1ms service=21ms status=403 bytes=380 protocol=https

如您所見,沒有任何有用的消息或解釋,只是說禁止。 我真的不知道問題可能是什么,但這里有一堆重要/相關的文件:

應用程序.js:

const express = require("express");

const configRoutes = require("./routes");
const static = express.static(__dirname + '/public');
const app = express();

app.use("/public", static);

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const cookieParser = require("cookie-parser");
app.use(cookieParser());

configRoutes(app);


app.listen(process.env.PORT || 3000, () => {
    console.log("The application is running on http://localhost:3000");

    if (process && process.send) process.send({done: true});
});

包.json:

{
  "name": "browserrpg",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "engines": {
    "node": "10.16.3"
  },
  "dependencies": {
    "angular": "^1.7.2",
    "angular-route": "^1.7.2",
    "body-parser": "^1.18.3",
    "cookie-parser": "^1.4.3",
    "express": "^4.16.3",
    "mongodb": "^3.1.1",
    "npm": "^6.2.0",
    "uuid": "^3.3.2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "repository": {
    "type": "git",
    "url": (git url here, removed for privacy)
  },
  "author": "",
  "license": "ISC",
}

簡介:

web: node app.js

如果相關,這里是 Heroku 調用的“/”路由:

const index = (req, res) => {
    res.sendFile(path.join(__dirname, "..\\public\\html", "index.html"));
    return;
}

這是設置所有路由的構造函數:

const constructorMethod = app => {
    app.get("/", index);
    app.get("/game", gameGet);
    app.post("/game", gamePost);

    app.use("*", (req, res) => {
        res.status(404).json({ error: "Not found" });
    });
  };

這也是我的文件結構:

BrowserRPG
│   README.md
│   Procfile
|   app.js
|   mongoCollections.js
|   mongoConnection.js
|   package.json
|   settings.json
│
└───data
    │   enemydata.js
    │   gamecalc.js
    |   gamedata.js
    |   index.js
    │
│   
└───public
    │   
    └───css
        |   main.css
    |
    └───html
        |   game.html
        |   index.html
    |
    └───js
        |   angular.js
        |   angularActiveGame.js
|
└───routes
    |   index.js

我也在使用 mongodb 數據庫,但我認為這不是問題的原因,因為我什至還沒有嘗試將它連接到 Heroku,而且您不需要運行 db 來訪問應用程序的第一頁。 這里有什么可能導致錯誤的嗎? 謝謝。

所以我終於想通了,解決方案實際上非常簡單。 在我的“/”GET 路由中,路徑包含一個.. ,它被視為惡意的,因此被拒絕。 我把它改成這樣:

res.sendFile(path.join(appRoot, "public/html", "index.html"));

appRoot是一個全局變量,指向應用程序的根目錄。 希望這可以幫助可能遇到類似問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM