简体   繁体   English

API 从 localhost 部署到 github 抛出 404 错误

[英]API deployed from localhost to github throw 404 error

I started my first api project and tested it in local host "http://localhost:3000/api/" and everything works on the browser.我开始了我的第一个 api 项目并在本地主机“http://localhost:3000/api/”中对其进行了测试,一切都在浏览器上运行。

But after deployed to github, and access "https://trily84.github.io/API_timestamp/api/" it throws 404 error.但是在部署到 github 后,访问“https://trily84.github.io/API_timestamp/api/”会抛出 404 错误。

The index.html is in the root folder "https://trily84.github.io/API_timestamp". index.html 位于根文件夹“https://trily84.github.io/API_timestamp”中。 The idea is to click on the 2 hyperlinks in the html as api working examples.想法是单击 html 中的 2 个超链接作为 api 工作示例。

I'm new to api and backend stuff so I'm obviously missing something?我是 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);
});

you cant expect github to launch your express server.你不能指望 github 启动你的快递服务器。 it wont.它不会。

github.io only serves static files. github.io 仅提供 static 文件。

Like @Raphael PICCOLO said, Github only serves static files.就像@Raphael PICCOLO 所说,Github 只提供 static 文件。 You cannot run a Nodejs application on Github.您不能在 Github 上运行 Nodejs 应用程序。 You can use this instead.您可以改用 I deployed my freecodecamp assignments on glitch and it worked.我在故障上部署了我的 freecodecamp 任务并且它有效。 You can see this link你可以看到这个链接

I don't understand why "trily84.github.io/API_timestamp/index.html" works?我不明白为什么“trily84.github.io/API_timestamp/index.html”有效? but not "localhost:3000/index.html"但不是“localhost:3000/index.html”

In your folder, you have the index.html file and Github serves this file (and it does nothing with your nodejs code).在您的文件夹中,您有index.html文件和 Github 提供此文件(它与您的 nodejs 代码无关)。 The error in localhost is because you don't have "/index.html" route and the file index.html is not in public folder. localhost 中的错误是因为您没有“/index.html”路由并且文件index.html不在public中。 You can move index.html in public folder to see what happens:).您可以在public文件夹中移动index.html以查看会发生什么:)。 In that case, this part isn't necessary anymore.在这种情况下,这部分不再是必需的。

app.get("/", function (req, res) {
  res.sendFile(__dirname + '/index.html');
});

(This will handle "/" path, you have the content of index.html when visit http://localhost:3000) (这将处理“/”路径,当访问 http://localhost:3000 时,您有 index.html 的内容)

Thanks for the comments all.感谢大家的评论。 I ended up deployed to Heroku and it works!我最终部署到 Heroku 并且它有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM