Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从Nodejs中的文本字段获取输入数据_Node.js_Express_Body Parser - Fatal编程技术网

Node.js 从Nodejs中的文本字段获取输入数据

Node.js 从Nodejs中的文本字段获取输入数据,node.js,express,body-parser,Node.js,Express,Body Parser,我相信这个问题已经得到了回答,但我找不到确切的问题 我有一个ejs文件,其中包含表单的这个 <form action="" method="POST"> <div class="input-group"> <input type="text" class="form-control" name="userSearchInput" placeholder="Enter the id of the product you would like to b

我相信这个问题已经得到了回答,但我找不到确切的问题

我有一个ejs文件,其中包含表单的这个

<form action="" method="POST">
   <div class="input-group">
      <input type="text" class="form-control" name="userSearchInput" placeholder="Enter the id of the product you would like to buy" aria-label="Recipient's username" aria-describedby="basic-addon2">
      <div class="input-group-append">
           <button class="btn btn-outline-secondary" id="searchBTN" type="submit"><i class="fas fa-cart-plus mr-2"></i>Add to Cart</button>
    </div>
 </div>
</form>
我已经在这里为body parser设置了中间件:

// middleware for bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
然后,为了将用户键入的内容输入到输入文本框中,我使用以下方法:

app.post('/', function(req, res) {
    var item = req.body.userSearchInput;
    console.log(item);
});
这是我第一次使用app.post,由于没有任何控制台记录,我不确定我哪里出了问题

完整的app.js文件

var express = require('express');
var path = require('path');
var http = require('http');
var mysql = require('mysql');
var bodyParser = require('body-parser');
var nodemon = require('nodemon');


var app = express();

var port = process.env.PORT || 3000;

// setting up views
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));

// middleware for bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// create connection to mySQL db
var connection = mysql.createConnection ({
    host     : 'localhost',
    user     : 'root',
    password : 'root',
    database : 'bamazon'
});

// initialize connection
connection.connect();

// run db query and print items to index html home page
connection.query('SELECT * from products', function (error, results) {
    if (error) throw error;
    console.log(results);
    app.get('/', function(req, res){
        res.render('index', {list: results});
    })
});

app.post('/', function(req, res) {
    var item = req.body.userSearchInput;
    console.log(item);
});


// setting up listen for server function
app.listen(port, function (err) {
    if (err) throw err;
    console.log("Server is running on port " + port);
});
使用


首先在您的ejs文件中添加表单操作,如action=“/search”。步骤2:尝试使用app.post(“/search”

工作正常我只是注释掉了数据库连接。
可能需要在新的express项目中尝试此app.js文件。
在npm启动的瞬间运行命令节点应用程序


此表单是从app.get(“/”)路由呈现的?我的意思是当前链接是根?是的。只有一个html页面,这是根页面。表单位于根页面上。是否确实添加了app。在定义路由之前使用(bodyParser…)?如果是真的,请尝试删除操作“”(这意味着将向当前url发送post请求)是的。我将用我的完整app.js文件更新我的帖子。为什么app.get(“/”)在查询回调中?这对我来说没有意义。没有工作。没有控制台。在浏览器或CL中记录输入的项目
var express = require('express');
var path = require('path');
var http = require('http');
var mysql = require('mysql');
var bodyParser = require('body-parser');
var nodemon = require('nodemon');


var app = express();

var port = process.env.PORT || 3000;

// setting up views
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));

// middleware for bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// create connection to mySQL db
var connection = mysql.createConnection ({
    host     : 'localhost',
    user     : 'root',
    password : 'root',
    database : 'bamazon'
});

// initialize connection
connection.connect();

// run db query and print items to index html home page
connection.query('SELECT * from products', function (error, results) {
    if (error) throw error;
    console.log(results);
    app.get('/', function(req, res){
        res.render('index', {list: results});
    })
});

app.post('/', function(req, res) {
    var item = req.body.userSearchInput;
    console.log(item);
});


// setting up listen for server function
app.listen(port, function (err) {
    if (err) throw err;
    console.log("Server is running on port " + port);
});
<form action="/" method="post">
var express = require('express');
var path = require('path');
var http = require('http');
var bodyParser = require('body-parser');

// var nodemon = require('nodemon');
// var mysql = require('mysql');

var app = express();

var port = process.env.PORT || 3000;

// setting up views
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));

// middleware for bodyParser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/*
 create connection to mySQL db
 var connection = mysql.createConnection({
     host: 'localhost',
    user: 'root',
    password: 'root',
     database: 'bamazon'
 });

// initialize connection
 connection.connect();

// run db query and print items to index html home page
 connection.query('SELECT * from products', function (error, results) {
     if (error) throw error;
     console.log(results);

 });
*/
app.get('/', function (req, res) {
    res.render('index', { list: [], title:"salman" });
})


app.post('/', function (req, res) {
    var item = req.body.userSearchInput;
    console.log(item);
});


// setting up listen for server function
app.listen(port, function (err) {
    if (err) throw err;
    console.log("Server is running on port " + port);
});