[英]router.get('/page') express Router not working
I am trying to make a simple express routing and I am using vanilla HTML for the front-end.我正在尝试制作一个简单的快速路由,并且我在前端使用 vanilla HTML。
index.js索引.js
const express = require('express'),
bodyParser = require('body-parser'),
serverless = require('serverless-http'),
app = express(),
nav = require('./routes/nav'), // the router
PORT = process.env.PORT || 8080;
/* Settings */
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('./public'));
/* APIS */
app.use('/.netlify/functions/api', nav); //Navigation API
module.exports = app;
module.exports.handler = serverless(app);
if(process.env.NODE_ENV === "development"){
app.listen(PORT, () => {
console.log(`Server is runing on PORT: ${PORT} ENV: ${process.env.NODE_ENV}`);
});
}
nav.js导航.js
const express = require('express');
const router = express.Router();
const PATH = "../../public/";
router.get('/', (req, res) => {
res.render(`${PATH}index.html`)
});
router.get('/graph', (req, res) => {
res.render(`${PATH}pages/graph.html`)
})
module.exports = router;
every time I try to route using the tag it tells me "Can not get /graph"每次我尝试使用标签进行路由时,它都会告诉我“无法获取 /graph”
my HTML nav我的 HTML 导航
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
<li class="nav-item mr-auto"><a class="nav-link" href="/graph">Graph Algorithims</a></li>
<li class="nav-item"><a class="nav-link" href="/sort">Sorting Algorithims</a></li>
</ul>
I thought the problem might be on the local server and I tried deploying to netlify to see if it works there and the same problem occurs.我认为问题可能出在本地服务器上,我尝试部署到 netlify 以查看它是否在那里工作,并且发生了同样的问题。
Reading your code, I can't seem to see where you "connect" the router.js file into your index.js.阅读您的代码,我似乎看不到您将 router.js 文件“连接”到 index.js 的位置。
To fix this, add the following in index.js:要解决此问题,请在 index.js 中添加以下内容:
const router = require('path/to/router.js');
app.use(router);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.