You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express.js出现Cannot GET /register问题:/login正常/register异常排查

问题解答:Cannot GET /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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:00:11