最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Node.JS and express

网站源码admin24浏览0评论

Node.JS and express

Node.JS and express

我的POST功能有问题。当我按下登录按钮时,出现以下消息:

无法发布/登录

我不知道该怎么办。我希望你们中的一个可以帮助我\(^-^ \)

这是我的代码:

    const express = require('express')
    const app = express()
    const { check } = require('express-validator')
    const http = require('http')
    const port = 3000

    app.use(express.static('./public', {index: 'login.html'}))
    app.use(express.urlencoded());

    // Routing
    let loginRouter = express.Router()

    app.use('/login', loginRouter)

    loginRouter.all('/login', function (req, res) {
        console.log('POST')
        console.log(req.body)
        console.log('GET')
        console.log(req.query)
    })
    app.listen(port, function() {
        console.log('listen on http://localhost:' + port)
    })
<script src=".6.3/umd/react.production.min.js"></script>
<script src=".6.3/umd/react-dom.production.min.js"></script>
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Login 1</title>
        <link rel="stylesheet" href="login.css">
    </head>
    <body>
        <form method="post" action="/login">
            <div>
                <label for="user">Username:</label>
                <input type="text" id="user" />
            </div>
            <div>
                <label for="userpw">Password:</label>
                <input type="password" id="userpw" />
            </div>
            <div>
                <button type="submit">Log in</button>
            </div>
        </form>
    </body>
</html>
回答如下:

您使用以下方法在路径loginRouter中将/login注册为中间件:

app.use('/login', loginRouter)

这意味着您在路由器中侦听的任何路径都将成为/login的子路径。

因此,您的实际链接将变为/login/login

您可能想要删除上述中间件并重试。

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论