Javascript 时间戳REST API端点链接显示404错误未找到
我刚开始学习。我在本地与postman和localhost进行了测试,所有测试似乎都有效。但当我上传到github时,单击index.html中的API端点超链接会抛出404错误。我可能错过了什么 任何关于为什么会发生这种情况的帮助都会很好Javascript 时间戳REST API端点链接显示404错误未找到,javascript,node.js,api,rest,express,Javascript,Node.js,Api,Rest,Express,我刚开始学习。我在本地与postman和localhost进行了测试,所有测试似乎都有效。但当我上传到github时,单击index.html中的API端点超链接会抛出404错误。我可能错过了什么 任何关于为什么会发生这种情况的帮助都会很好 //index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <
//index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Timestamp and Microservices</title>
<script src="./server.js"></script>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<container>
<h2 id="title">Timestamp and Microservices</h2>
<h4>Example Usage:</h4>
<a href="api/2015-12-25">[project url]/api/2015-12-25</a><br>
<a href="api/1451001600000">[project url]/api/1451001600000</a>
<h4>Example Output:</h4>
<p>{"unix":1451001600000, "utc":"Fri, 25 Dec 2015 00:00:00 GMT"}</p>
</container>
</body>
</html>
它不工作,因为我部署到只支持静态文件的github。所以我最终部署到了Heroku,它成功了 你部署了这个代码吗?您的后端是否正在运行并打印此行
你的应用正在监听端口
当我在VScode的命令行中键入“node server.js”时,它显示:“你的应用正在监听端口3000”。http://localhost:3000/index.html
在浏览器上点击这个按钮会得到什么?我运行nodemon server.js,然后输入。我得到了“Cannot GET/index.html”,我希望你的根目录中有一个公共文件夹,里面有一个名为index.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) => {
let datestring = req.params.datestring
let date
// if datestring is empty
if (!datestring) {
date = new Date()
}
// if datestring is not empty
else {
// if datestring is a number string
if (!isNaN(datestring)) {
// convert it to interger
datestring = parseInt(datestring)
date = new Date(datestring)
// res.json({datestring: datestring, typeofdatestring: typeof(datestring), date: typeof(date)})
}
// if datestring is a string like 2020-01-01
else {
date = new Date(datestring)
// res.json({datestring: datestring, typeofdatestring: typeof(datestring), date: typeof(date)})
}
}
// determine if date is a string or an integer
if (date.toString() === "Invalid Date") {
res.json({error: date.toString()})
}
else {
res.json({Unix: date.getTime(), UTC: date.toUTCString()})
}
})
var listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});