Express.js出现Cannot GET /register问题:/login正常/register异常排查
Hey there! I spotted the issue right away—your /register routes have a syntax error that's preventing them from being registered properly.
Looking at your route file, the way you've written router.get('/register') and router.post('/register') has misplaced parentheses. You closed the router.get()/router.post() call too early, so the callback function never gets attached to the route. That's why Express doesn't recognize /register when you try to access it, while /login works fine because its syntax is correct.
修正后的路由文件
const express = require('express'); const router = express.Router() const bcrypt = require('bcrypt'); const user = require('../models/user.js') router.get('/',(req,res)=> res.render('home') ) router.get('/login',(req,res)=> res.render('login') ) // 修正括号位置:将回调函数作为第二个参数传入router.get() router.get('/register',(req,res)=>{ res.render('register') }) // 同样修正POST路由的括号问题 router.post('/register', (req, res) =>{ // 这里可以补充你的注册逻辑,比如表单验证、密码加密、用户入库等 }); module.exports = router
Once you fix those parentheses, your /register GET route will properly render the register view, and the POST route will be ready to handle form submissions. Double-check that your register.handlebars template exists in the correct views directory too, just to be safe!
内容的提问来源于stack exchange,提问作者ana bel

