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

qgelzfjb  于 2023-06-22  发布在  Node.js
关注(0)|答案(3)|浏览(191)

我相信这个问题已经有答案了,但我找不到我想问的问题。
我有一个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>

在我的app.js文件的节点端,我已经安装并下载了express和body-parser,并完成了必要的require函数。

var bodyParser = require('body-parser');
var express = require('express');
var app = express();

我在这里设置了我的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);
});
7ivaypg9

7ivaypg91#

使用

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

qyyhg6bp2#

首先在你的ejs文件中添加表单动作,比如action="/search”。第二步:尝试使用app.post('/search'

kwvwclae

kwvwclae3#

工作正常,我只是注解了数据库连接而已。
可能会尝试这个app.js文件与新的express项目。
运行命令node app instant of npm start

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);
});

相关问题