【问题标题】:node.js express - Can't see data from post form in post routenode.js express - 在发布路由中看不到来自发布表单的数据
【发布时间】:2020-05-17 13:31:29
【问题描述】:

嘿,我正在尝试在我的项目中使用 node.js 做一些基本的登录功能。但是我在使用路由器时遇到了一些问题。我似乎无法通过 html 发布表单在终端中发送/显示任何信息。由于某种原因,它只能通过 POSTMAN 工作。我也无法向 auth.js 添加任何获取路由,然后当我尝试访问它时出现以下错误:

错误:ENOENT:没有这样的文件或目录,stat 'xxxx/路线'

如您所见,我已将登录获取路由移至 app.js,它可以正常工作。

所以归结为两件事:

  1. 为什么我不能从 login.html 获取我的表单数据并使用 auth.js 登录帖子路由显示它
  2. 为什么我不能将登录获取路由添加到 auth.js

这是 app.js

const express = require('express');
const app = express();

app.use(express.static("public"));
app.use(express.json());


app.get("/login", (req, res) => {

    return res.sendFile(__dirname + "/public/login.html");
});

const session = require('express-session');
const config = require('./config/config.json');
app.use(session({
    secret: config.sessionSecret,
    resave: false,
    saveUninitialized: true
}));

const rateLimit = require('express-rate-limit');

const limiter = rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100 // limit each IP to 100 requests per windowMs
});

app.use(limiter);

const authLimiter = rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 8 // limit each IP to 8 requests per windowMs
});

app.use('/signup', authLimiter);
app.use('/login', authLimiter);


/* Setup Knex with Objection */
const { Model } = require('objection');
const Knex = require('knex');
const knexfile = require('./knexfile.js');
const knex = Knex(knexfile.development);

Model.knex(knex);

const authRoute = require('./routes/auth.js');
const usersRoute = require('./routes/users.js');

app.use(authRoute);
app.use(usersRoute);

const PORT = 3000;

app.listen(PORT, (error) => {
    if (error) {
        console.log(error);
    }
    console.log("Server is running on the port", PORT);
})


这是我的 auth.js

const router = require('express').Router();

router.post('/login', (req, res) => {

    console.log(req.body)
    let userlogin = {
        username: req.body.uname,
        password: req.body.psw
      }

    console.log(userlogin);

    res.send({ response: userlogin });
});

module.exports = router;


这是公用文件夹中的 login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body{
            text-align: center;
            margin-top: 15%;
        }
        .container{
            padding: 5%;
            margin: 2%;
        }
    </style>
    <title>login</title>
</head>
<body>
    <h1>Login site</h1>
    <form  action="/login" method="POST">
        <div class="container">
          <label for="uname"><b>Username</b></label><br>
          <input type="text" placeholder="Enter Username" name="uname" ><br>

          <label for="psw"><b>Password</b></label><br>
          <input type="password" placeholder="Enter Password" name="psw" ><br><br>

          <button type="submit">Login</button><br><br>
        </div>
    </form> 

    <div>
        <form action="/signup.html">
            <input type="submit" value="signup" />
        </form>
    </div>
</body>
</html>

【问题讨论】:

    标签: html node.js forms express post


    【解决方案1】:

    程序建议您使用位于公用文件夹中的静态页面。相反,您需要使用模板来使用登录凭据。

    去 EJS。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 2011-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多