简体   繁体   English

无法进入Node Js

[英]Cannot Get in Node Js

I am new to node js and creating a simple application to query data stored in database(MySql).So what i am doing is, I have created a database named stock and i am querying it using index.html to show it at get.html but after executing the get request i am not able to get the result.我是节点 js 的新手,并创建了一个简单的应用程序来查询存储在数据库(MySql)中的数据。所以我正在做的是,我创建了一个名为 stock 的数据库,我正在使用 index.html 查询它以在获取时显示它。 html 但在执行获取请求后我无法获得结果。 Here is my app.js这是我的 app.js

const express=require('express');
const app=express();
const port= 5050;
const bodyParser=require("body-parser");

app.use(bodyParser.urlencoded({extended:false}));

app.get('/',(req,res)=>res.sendFile(__dirname + '/index.html'));

app.post('/get',function(req,res){
const mysql=require('mysql');


const con=mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"abc123",
    database:"abc",
});

con.connect(function(err){
    if(err) throw err;
    console.log("Connected");
    let sqlQuery='SELECT * FROM stock';
con.query(sqlQuery,(err,rows)=>{
    if(err) throw err;
    console.log('Data Received:-');
    console.log(rows);


        });
    });
});
app.listen(port);

My Index.html:-我的索引.html:-

<!DOCTYPE html>
<html>
<head>
    <title>My node js app</title>
</head>
<body>
    <form action="/get" method="get">
        <h1>Welcome to Stock manipulation</h1><br></br>
        Select option<select>
        <option value=0>Get</option></select>
        <input type="submit" id="query" value="get Result">
</body>
</html>

And my get.html还有我的get.html

<!DOCTYPE html>
<html>
<head>
    <title>Get</title>
</head>
<body>

</body>
</html>

And here is the data stored at database这是存储在数据库中的数据

[ RowDataPacket { id: 1, type: 'BSE' },
  RowDataPacket { id: 2, type: 'NSE' } ]

The error i am getting after Submitting the request is提交请求后我得到的错误是在此处输入图像描述

change改变

<form action="/get" method="get">

to

<form action="/get" method="post">

as you have defined the /get route ( app.post('/get',function(req,res){/*..*/}) )正如您已定义/get路线( app.post('/get',function(req,res){/*..*/})

to accept only post requests只接受post请求

Also in your /get route handler you should output something.同样在您的/get路由处理程序中,您应该 output 一些东西。 Right now you do not output anything, only log to the node.js console现在你没有 output 任何东西,只登录到 node.js 控制台

What is your nodejs server saying?你的nodejs服务器在说什么? In your routes you typically want to return some data.在您的路线中,您通常希望返回一些数据。 for example in your case your /get route res.send(data).例如,在您的情况下,您的 /get route res.send(data)。 that way your front end can show the data received.这样您的前端就可以显示收到的数据。 Also looks like you need to change your form to be a post not a get (Edit: As Nikos M. mentioned).看起来您还需要将表单更改为帖子而不是获取(编辑:正如 Nikos M. 提到的)。

If you are new to http requests I recommend downloading Postman to get used to testing your routes requests.如果您不熟悉 http 请求,我建议您下载 Postman 以习惯测试您的路线请求。

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

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