【发布时间】:2020-08-24 07:48:17
【问题描述】:
我有一个带有名为“关于”的链接的 index.html。单击 About 时,我只收到错误 Cannot GET /about。这里有什么问题?
我的代码:
const express = require('express');
const app = express();
const path = require('path');
const router = express.Router();
router.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/index.html'));
});
router.get('/about',function(req,res){
res.sendFile(path.join(__dirname+'/about.html'));
});
app.use('/', router);
app.listen(process.env.port || 3000);
console.log('Running at Port 3000');
在 index.html 中:
<a href="/about">About</a>
【问题讨论】:
-
你能展示一下你用过的
<a>吗? -
@ChrisG 关于
-
使用您的确切代码对我来说很好。地址栏是
http://localhost:3000/about吗? -
@ChrisG 现在似乎可以工作了..奇怪:D
-
添加
/about路由后是否重启了服务器?
标签: javascript express