Javascript router.get不';不起作用,但使用id的router.get起作用
打电话可以工作,并将我重定向到 surveypage.html 但调用不起作用,它会重定向公用文件夹中的主index.js文件,甚至不会打印“常规获取” 为什么会这样 surveypage.js在路由中Javascript router.get不';不起作用,但使用id的router.get起作用,javascript,express,web-applications,routes,Javascript,Express,Web Applications,Routes,打电话可以工作,并将我重定向到 surveypage.html 但调用不起作用,它会重定向公用文件夹中的主index.js文件,甚至不会打印“常规获取” 为什么会这样 surveypage.js在路由中 const path = require("path"); const router = express.Router(); router.use(express.static(path.join(__dirname, "../public"))); router.get('/:id', a
const path = require("path");
const router = express.Router();
router.use(express.static(path.join(__dirname, "../public")));
router.get('/:id', async (req, res) => {
console.log("get with id");
res.sendFile(path.join(__dirname, "../public/Surveypage.html"));
})
router.get('/', async (req, res) => {
console.log("regular get");
res.sendFile(path.join(__dirname, "../public/Surveypage.html"));
})
module.exports = router; ```
==============================================================================
index.js of node:
const surveyPage = require('./routes/surveypage')
const app = express();
app.use(express.json());
app.use('/surveypage',surveyPage)
app.use(express.static(path.join(__dirname, "/public")));
app.use(cors())
const PORT = process.env.PORT || 5000
app.listen(PORT , () => {
console.log(`Listenning on porst ${PORT }`);
})
确保文件的结构与代码一致 以下代码对我来说运行良好: app.js:
const express=require(“express”);
常量app=express();
const cors=要求(“cors”)
const surveyPage=require(“./routes/surveyPage”)
常量路径=要求(“路径”);
const PORT=process.env.PORT | 5000
使用(express.json());
应用程序使用('/surveypage',surveypage)
//app.use(express.static(path.join(uu dirname,“/public”));//不需要
app.use(cors())
app.listen(端口,()=>console.log(`Listenning on porst${PORT}`))
surveypage.js
const path=require(“路径”);
const express=require('express')
const router=express.router();
router.use(express.static(path.join(uu dirname,“../public”));
router.get('/:id',(req,res)=>res.sendFile(path.join(uu dirname,“../public/Surveypage.html”);
router.get('/',(req,res)=>res.sendFile(path.join(uu dirname,“../public/Surveypage.html”))
module.exports=路由器;