[英]API deployed from localhost to github throw 404 error
我開始了我的第一個 api 項目並在本地主機“http://localhost:3000/api/”中對其進行了測試,一切都在瀏覽器上運行。
但是在部署到 github 后,訪問“https://trily84.github.io/API_timestamp/api/”會拋出 404 錯誤。
index.html 位於根文件夾“https://trily84.github.io/API_timestamp”中。 想法是單擊 html 中的 2 個超鏈接作為 api 工作示例。
我是 api 和后端的新手,所以我顯然遺漏了什么?
//index.html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<a href="api/2015-12-25">[project url]/api/2015-12-25</a><br>
<a href="api/1451001600000">[project url]/api/1451001600000</a>
</html>
//server.js
var express = require('express');
var app = express();
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/api/:datestring?', (req, res) => { code block })
var listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});
你不能指望 github 啟動你的快遞服務器。 它不會。
github.io 僅提供 static 文件。
就像@Raphael PICCOLO 所說,Github 只提供 static 文件。 您不能在 Github 上運行 Nodejs 應用程序。 您可以改用它。 我在故障上部署了我的 freecodecamp 任務並且它有效。 你可以看到這個鏈接
我不明白為什么“trily84.github.io/API_timestamp/index.html”有效? 但不是“localhost:3000/index.html”
在您的文件夾中,您有index.html
文件和 Github 提供此文件(它與您的 nodejs 代碼無關)。 localhost 中的錯誤是因為您沒有“/index.html”路由並且文件index.html
不在public
中。 您可以在public
文件夾中移動index.html
以查看會發生什么:)。 在這種情況下,這部分不再是必需的。
app.get("/", function (req, res) {
res.sendFile(__dirname + '/index.html');
});
(這將處理“/”路徑,當訪問 http://localhost:3000 時,您有 index.html 的內容)
感謝大家的評論。 我最終部署到 Heroku 並且它有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.