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

时间:2018-04-05 20:57:36

标签: 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 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();

我在这里为身体解析器设置了我的中间件:

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

3 个答案:

答案 0 :(得分:0)

使用

/** IE9, IE10 and IE11 requires all of the following polyfills. **/
// import 'core-js/es6/symbol';
// import 'core-js/es6/object';
// import 'core-js/es6/function';
// import 'core-js/es6/parse-int';
// import 'core-js/es6/parse-float';
// import 'core-js/es6/number';

答案 1 :(得分:0)

首先在您的ejs文件中添加表单操作,例如action =&#34; / search&#34;。第2步:尝试使用 app.post(&#39; / search&#39; < / p>

答案 2 :(得分:0)

  

工作正常我只是注释掉数据库连接   可以尝试这个带有新快递项目的app.js文件   运行npm start的命令节点app instant

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