router.get('/page') express 路由器不工作



我正在尝试制作一个简单的快速路由,并且我使用香草HTML作为前端。

index.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

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;

每次我尝试使用标签路由时,它告诉我";无法获取/图形";

我的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>

我认为问题可能出现在本地服务器上,我尝试部署到netlify,看看它是否在那里工作,但也出现了同样的问题。

阅读您的代码,我似乎看不到您在哪里"连接";将router.js文件插入到index.js.中

要修复此问题,请在index.js:中添加以下内容

const router = require('path/to/router.js');
app.use(router);

相关内容

  • 没有找到相关文章

最新更新