繁体   English   中英

提交表单时出错:无法使用 NodeJS + ExpressJS 发布

[英]Error when submitting form: Cannot POST using NodeJS + ExpressJS

我是新学习 NodeJS + Express 的新手,现在我正在尝试构建一个简单的注册表单,但我一直在使用此表单和其他表单时遇到相同的错误:

无法 POST /registerauth

我在 stackoverflow 和其他网站上查看了数十个类似的问题,但没有找到适用于我的案例的答案。

这是表格:

<form id="register-form" class="panel-form" action="/registerauth" method="POST">
          <input type="text" name="register-username" id="register-username" class="fill-input" placeholder="Username *" autofocus="true" maxlength="15" required>

          <input type="password" name="register-password" id="register-password" class="fill-input" placeholder="Password *" maxlength="30" required>


                        <button type="submit">Register</button>
                    </form>

我的 app.js 文件:

const express = require('express');
const app= express();
const path = require('path');
const port = process.env.PORT || 3000;
const login = require('./routes/login'); /*MODULE THAT HAS THE CONTROLLER CODE*/

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname , 'public')));

app.post('/registerauth',function (req,res,next){  /*TRIED THIS BUT DIDN'T WORK*/
    console.log("testing");
    res.json(req.body);
})

app.use('/login', login); /*TRIED CALLING THE MODULE THAT HAS THE CONTROLLER AND DELETING THE LINE ABOVE BUT DIDN'T WORK*/

app.listen(port, ()=> console.log(`server started on port ${port} `))

具有控制器代码但它甚至没有被调用的模块:

const express    = require('express');
const oracledb    = require('oracledb');
const router        = express.Router();
const dbConfig =require('../dbconfig.js') ;

class Cliente{
    constructor(username,password,nombre,email){
        this.username = username;
        this.password=password;
        this.nombre=nombre;
        this.email=email;
    }
}

    let conexion;

    router.post('/registerauth',async(req,res,next)=>{ /*all this is not working neither*/

        try{
            console.log("THIS IS NOT WORKING");

            cliente = new Cliente(req.body.username, req.body.password,req.body.nombre,req.body.email);

     conexion= await oracledb.getConnection(dbConfig);
     const result = await conexion.execute(
        `INSERT INTO Cliente values (${cliente.username}, ${cliente.password},
 ${cliente.nombre}, ${cliente.email})`
     );
} catch(err){
    console.error(err);
}finally{
    conexion.close();
}
    })

module.exports = router;

我的项目文件夹是这样构建的:

/
 node_modules
 public/
       css/
       media/
       scripts/
       index.html (just the file inside public folder)
       register.html (just the file inside public folder THIS IS THE REGISTER FORM FILE)
 routes/
       api/
          login.js
 app.js
 dbconfig.js
 package-lock.json
 package.json

注意:我在我的项目中使用不同的操作方法创建了其他表单,并且所有表单都给出了相同的错误

这是整个想法:

 const express = require("express") const app = express() const router = require("./router") app.use(express.static("public")) app.use(express.urlencoded({extended: false})) app.use(express.json()) app.use('/', router) // write all your routes in this file 'router' module.exports = app

您需要在服务器中的端口“3000”上呈现 HTML 文件。 我认为您正在尝试在服务器外部呈现 html 页面时直接访问“ http://localhost:3000/registerauth ”。

对 App.js 文件进行这些更改

const express = require('express');
const app= express();
const path = require('path');
const port = process.env.PORT || 3000;
const login = require('./routes/login'); /*MODULE THAT HAS THE CONTROLLER CODE*/

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

//newly added code
app.get('/',(req,res)=>{
    res.sendFile('index.html');

})

app.post('/registerauth',function (req,res,next){  /*TRIED THIS BUT DIDN'T WORK*/
    console.log("testing");
    res.json(req.body);
    // Redirect to '/login' here instead of sending response which will trigger the login module
})

app.use('/login', login); /*TRIED CALLING THE MODULE THAT HAS THE CONTROLLER AND DELETING THE LINE ABOVE BUT DIDN'T WORK*/

app.listen(port, ()=> console.log(`server started on port ${port} `))

当您点击“ http://localhost:3000 ”时呈现 html 页面。 此外,为了让您的登录组件正常工作,您需要重定向到 POST 请求中的“/login/”路径

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM