API从本地主机部署到github抛出404错误



我开始了我的第一个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启动您的express服务器。它不会。

github.io只提供静态文件。

正如@Raphael PICCOLO所说,Github只提供静态文件。你不能在Github上运行Nodejs应用程序。你可以用这个来代替。我把我的免费代码营任务部署在故障上,它起了作用。你可以看到这个链接

我不明白为什么"trily84.github.io/API_timestamp/index.html";作品但不是";localhost:3000/index.html";

在你的文件夹中,你有index.html文件,Github提供这个文件(它对你的nodejs代码没有任何作用(。localhost中的错误是因为您没有"/index.html";路由并且文件CCD_ 2不在CCD_。您可以在public文件夹中移动index.html,查看会发生什么:(。在这种情况下,这个部分就没有必要了。

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

(这将处理"/"路径,访问时您有index.html的内容http://localhost:3000)

感谢大家的评论。我最终被部署到了赫罗库,这很有效!

相关内容