req.body 在 post express nodejs 上未定义
req.body undefined on post express nodejs
<form name="tld_ingest" method="POST" enctype="multipart/form-data">
<div class="form-row">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Application Name</label>
<input type="text" class="form-control" id="namel" name="namel" placeholder="Application Name">
</div>
<input type="submit" id="runall" formaction="/runAll" class="btn btn-primary btn-sm nine" style="margin-left: 40%; width: 60px; height: 35px; margin-top: 32px;" value="Run All" />
</div>
<div class="form-row">
<div class="form-group col-sm-2">
<label for="inputEmail4">Use Case</label>
<select class="form-control" id="mof" name="mof">
<option value="s3">S3</option>
<option value="db">Database</option>
<option value="file">File</option>
</select>
</div>
<div class="form-check seven" style="margin-left:8%; margin-top:25px;">
<input class="form-check-input" type="radio" name="radio" id="radio" value="option2">
<label class="form-check-label" for="exampleRadios2" style="overflow-wrap: normal;white-space: nowrap;">
Ignore in-line DQ
</label>
</div>
<!-- <input type="button" id="btnloading" class="btn btn-primary btn-sm eight" style="margin-left: 23%; width: 60px; height: 35px; margin-top: 35px;" onclick="run_button()" value="Run" /> -->
</div>
<div class="form-row fifth" id="area3">
<div class="form-group col-sm-2">
<label for="line" style="overflow-wrap: normal;white-space: nowrap;">Line of Business</label>
<input type="text" class="form-control" id="line" name="line" placeholder="Insights & Data">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Distributed To</label>
<select class="form-control" id="dist" name="dist">
<option>EDL-WLDS</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4">Environment</label>
<select class="form-control" id="env" name="env">
<option>UAT</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
<div class="form-row fifth" id="area4">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Select Tool</label>
<select class="form-control" id="data" name="data">
<!-- <option>Abinitio</option>
<option>Spark</option> -->
<option>Talend Data Fabric</option>
</select>
</div>
<div class="form-group col-sm-2 cord">
<label for="inputAddress" style="overflow-wrap: normal;white-space: nowrap;">Source Name</label>
<input type="text" class="form-control" id="source" name="source" placeholder="Source Name">
</div>
<!-- <div class="form-group col-sm-2 cord">
<label for="inputAddress">Line of Business</label>
<select class="form-control">
<option>Insights & Data</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div> -->
<div class="form-group col-sm-2 cord">
<label for="inputAddress" style="overflow-wrap: normal;white-space: nowrap;">Source Owner</label>
<input type="text" class="form-control" id="own" name="own" placeholder="Source Owner">
</div>
</div>
<div class="form-row third" id="area1">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">AWS Access Key</label>
<input type="text" class="form-control" id="key" name="key" placeholder="Access Key">
</div>
<div class="form-group col-sm-2 cord">
<label for="password" style="overflow-wrap: normal;white-space: nowrap;">AWS Secret Key</label>
<input type="password" class="form-control" id="secret" name="secret" placeholder="Secret Key">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">AWS Bucket Name</label>
<input type="text" class="form-control" id="bucket" name="bucket" placeholder="Bucket Name">
</div>
</div>
<div class="form-row third" id="area2">
<div class="form-group col-sm-2 ">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Key</label>
<input type="text" class="form-control" id="fkey" name="fkey" placeholder="File Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Delimiter</label>
<input type="text" class="form-control" id="delim" name="delim" placeholder="Pipe">
</div>
<div class="form-group col-sm-2 cord">
<label for="header" style="overflow-wrap: normal;white-space: nowrap;">Is Header Present?</label>
<select class="form-control" id="opt" name="opt">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</div>
<div class="form-row third" id="area2">
<div class="form-group col-sm-2 key" style="display:none;">
<label for="header" style="overflow-wrap: normal;white-space: nowrap;">comma separated header</label>
<input type="text" class="form-control" id="text9" name="text9" placeholder="Header Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Target Table Name</label>
<input type="text" class="form-control" id="text0" name="text0" placeholder="Hive Table Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Target Database Name</label>
<input type="text" class="form-control" id="text03" name="text03" placeholder="Database Name">
</div>
</div>
<button type="button" class="btn btn-primary btn-sm third">Cancel</button>
<input type="submit" formaction="/submit" id="submit" class="btn btn-primary btn-sm third" style="float:right; margin-right: 37%; display: none;" value="Save & Run" />
<div class="form-row sixth" id="area3" style="display:none;">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Line of Business</label>
<input type="text" class="form-control" id="lob" name="lob" placeholder="Insights & Data">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Distributed To</label>
<select class="form-control" id="distr" name="distr">
<option>EDL-WLDS</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4">Environment</label>
<select class="form-control" id="envir" name="envir">
<option>UAT</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
<div class="form-row sixth" id="area4" style="display:none;">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Select Tool</label>
<select class="form-control" id="fab" name="fab">
<!-- <option>Abinitio</option>
<option>Spark</option> -->
<option>Talend Data Fabric</option>
</select>
</div>
<div class="form-group col-sm-2 cord">
<label for="inputAddress" style="overflow-wrap: normal;white-space: nowrap;">Source Name</label>
<input type="text" class="form-control" id="srcn" name="srcn" placeholder="Source Name">
</div>
<!-- <div class="form-group col-sm-2 cord">
<label for="inputAddress">Line of Business</label>
<select class="form-control">
<option>Insights & Data</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div> -->
<div class="form-group col-sm-2 cord">
<label for="inputAddress" style="overflow-wrap: normal;white-space: nowrap;">Source Owner</label>
<input type="text" class="form-control" id="owner" name="owner" placeholder="Source Owner">
</div>
</div>
<div class="form-row fourth" id="area11" style="display:none;">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Input File Path</label>
<input type="text" class="form-control" id="text1" name="text1" placeholder="Path">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">File Name</label>
<input type="text" class="form-control" id="text2" name="text2" placeholder="File Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Delimiter</label>
<input type="text" class="form-control" id="text3" name="text3" placeholder="Delimiter">
</div>
</div>
<div class="form-row fourth" id="area11" style="display:none;">
<div class="form-group col-sm-2">
<label for="header" style="overflow-wrap: normal;white-space: nowrap;">Is Header Present?</label>
<select class="form-control" id="arc" name="arc">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
<div class="form-group col-sm-2 key cord" style="display:none;">
<label for="header" style="overflow-wrap: normal;white-space: nowrap;">comma separated header</label>
<input type="text" class="form-control" id="sep" name="sep" placeholder="Header Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Target Table name</label>
<input type="text" class="form-control" id="targetb" name="targetb" placeholder="Hive Name">
</div>
</div>
<div class="form-row fourth" id="area11" style="display:none;">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Target Database name</label>
<input type="text" class="form-control" id="targetdb" name="targetdb" placeholder="Database Name">
</div>
</div>
<button type="button" class="btn btn-primary btn-sm fourth" style="display:none;">Cancel</button>
<input type="submit" id="submit1" formaction="/file" name="save_button" class="btn btn-primary btn-sm fourth" style="float:right; margin-right: 37%;" value="Save & Run" />
我正在尝试向数据库提交表单。这些值没有被提交,而是在数据库上我得到了未定义。基本上我的 req.body 是未定义的,不会传入值。
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var mysql = require('mysql');
var http = require('http');
//var formidable = require("express-formidable");
var path = require("path");
var shell = require('shelljs');
var formidable = require('formidable');
app.use(express.static('Project'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
var mysqlConnection = getConnection();
function getConnection() {
return mysql.createConnection({
host: 'localhost',
user: 'root',
port: '3306',
database: 'abc',
multipleStatements: true
});
}
app.post('/submit', (req, res) => {
console.log("entered");
console.log('line', req.body.line);
var form = new formidable.IncomingForm();
var application = req.body.namel;
console.log("appli", req.body.namel);
var useCase = req.body.mof;
console.log("value in use case", req.body.mof);
var line = req.body.line;
var accessKey = req.body.key;
var distributed = req.body.dist;
console.log("distributed", req.body.dist);
var environment = req.body.env;
console.log("envir", req.body.env);
var tool = req.body.data;
var sname = req.body.source;
console.log("source", req.body.source);
var owner = req.body.own;
var secretKey = req.body.secret;
var bucketName = req.body.bucket;
var fileName = req.body.fkey;
var delimiter = req.body.delim;
delimiter = '|';
var comma = req.body.text9;
var header = req.body.opt;
var tableName = req.body.text0;
console.log("fileName", fileName);
console.log("delimiter", delimiter);
console.log("header", header);
console.log("tableName", tableName);
var dbName = req.body.text03;
var nextVal;
var sql = 'select ifnull(max(id),0)+1 as nextJobId from talendfw';
var query = mysqlConnection.query(sql, (err, results) => {
if (err) throw err;
nextVal = results[0].nextJobId;
console.log(nextVal);
var queryString = "INSERT INTO talendfw (ID, Application_Name, t_key, t_value) VALUES('" + nextVal + "','" + useCase + "', 'line_of_business','" + line + "'), ('" + nextVal + "','" + useCase + "', 'distributed_to','" + distributed + "'), ('"+nextVal+"','" + useCase + "', 'environment','" + environment + "'),('"+nextVal+"','" + useCase + "', 'select_tool','" + tool + "'),('"+nextVal+"','" + useCase + "','source_name','" + sname + "'), ('"+nextVal+"','" + useCase + "', 'source_owner','" + owner + "'), ('"+nextVal+"','" + useCase + "', 'aws_access_key','" + accessKey + "'), ('"+nextVal+"','" + useCase + "', 'aws_secret_key','" + secretKey + "'), ('"+nextVal+"','" + useCase + "', 'aws_bucket_name','" + bucketName + "'), ('"+nextVal+"','" + useCase + "', 'key','" + fileName + "'), ('"+nextVal+"','" + useCase + "', 'delimiter','" + delimiter + "'), ('"+nextVal+"','" + useCase + "', 'is_header_present','" + header + "'), ('"+nextVal+"','" + useCase + "', 'comma_separated_header','" + comma + "'), ('"+nextVal+"','" + useCase + "', 'target_table_name','" + tableName + "'), ('"+nextVal+"','" + useCase + "', 'target_database_name','" + dbName + "')";
console.log("insert_query " + queryString);
mysqlConnection.query(queryString, (err, results, fields) => {
if (err) {
console.log("Failed to insert data" + err)
res.sendStatus(500);
return;
} else {
console.log("Inserted the values to DB successfully. ready for next operation", results);
shell_script(nextVal);
}
console.log("Operation completed");
});
请帮我解决这个问题。我使用了 express-formidable npm 而不是 req.body 我使用了 req.fields ,它工作正常但是与另一个使用强大的 npm 的表单数据有冲突。
您的表单元素具有此属性:
enctype="multipart/form-data"
这意味着您发送的请求的内容类型是multipart/form-data。如果删除它,表单将使用默认内容类型,即 x-www-form-urlencoded。 Bodyparser 应该能够解析它。
如果您仍想使用 multipart/form-data(您可以查看 this 关于什么更好的问题),您必须处理某种程度上的其他形式。
顺便说一下,您的代码可能对 SQL injection 来说很脆弱。在进行查询时,我会非常小心地使用字符串连接。您也不必再使用 body-parser 来解析正文。它已经合并到 Express 中,这意味着你可以做
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
<form name="tld_ingest" method="POST" enctype="multipart/form-data">
<div class="form-row">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Application Name</label>
<input type="text" class="form-control" id="namel" name="namel" placeholder="Application Name">
</div>
<input type="submit" id="runall" formaction="/runAll" class="btn btn-primary btn-sm nine" style="margin-left: 40%; width: 60px; height: 35px; margin-top: 32px;" value="Run All" />
</div>
<div class="form-row">
<div class="form-group col-sm-2">
<label for="inputEmail4">Use Case</label>
<select class="form-control" id="mof" name="mof">
<option value="s3">S3</option>
<option value="db">Database</option>
<option value="file">File</option>
</select>
</div>
<div class="form-check seven" style="margin-left:8%; margin-top:25px;">
<input class="form-check-input" type="radio" name="radio" id="radio" value="option2">
<label class="form-check-label" for="exampleRadios2" style="overflow-wrap: normal;white-space: nowrap;">
Ignore in-line DQ
</label>
</div>
<!-- <input type="button" id="btnloading" class="btn btn-primary btn-sm eight" style="margin-left: 23%; width: 60px; height: 35px; margin-top: 35px;" onclick="run_button()" value="Run" /> -->
</div>
<div class="form-row fifth" id="area3">
<div class="form-group col-sm-2">
<label for="line" style="overflow-wrap: normal;white-space: nowrap;">Line of Business</label>
<input type="text" class="form-control" id="line" name="line" placeholder="Insights & Data">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Distributed To</label>
<select class="form-control" id="dist" name="dist">
<option>EDL-WLDS</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4">Environment</label>
<select class="form-control" id="env" name="env">
<option>UAT</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
<div class="form-row fifth" id="area4">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Select Tool</label>
<select class="form-control" id="data" name="data">
<!-- <option>Abinitio</option>
<option>Spark</option> -->
<option>Talend Data Fabric</option>
</select>
</div>
<div class="form-group col-sm-2 cord">
<label for="inputAddress" style="overflow-wrap: normal;white-space: nowrap;">Source Name</label>
<input type="text" class="form-control" id="source" name="source" placeholder="Source Name">
</div>
<!-- <div class="form-group col-sm-2 cord">
<label for="inputAddress">Line of Business</label>
<select class="form-control">
<option>Insights & Data</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div> -->
<div class="form-group col-sm-2 cord">
<label for="inputAddress" style="overflow-wrap: normal;white-space: nowrap;">Source Owner</label>
<input type="text" class="form-control" id="own" name="own" placeholder="Source Owner">
</div>
</div>
<div class="form-row third" id="area1">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">AWS Access Key</label>
<input type="text" class="form-control" id="key" name="key" placeholder="Access Key">
</div>
<div class="form-group col-sm-2 cord">
<label for="password" style="overflow-wrap: normal;white-space: nowrap;">AWS Secret Key</label>
<input type="password" class="form-control" id="secret" name="secret" placeholder="Secret Key">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">AWS Bucket Name</label>
<input type="text" class="form-control" id="bucket" name="bucket" placeholder="Bucket Name">
</div>
</div>
<div class="form-row third" id="area2">
<div class="form-group col-sm-2 ">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Key</label>
<input type="text" class="form-control" id="fkey" name="fkey" placeholder="File Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Delimiter</label>
<input type="text" class="form-control" id="delim" name="delim" placeholder="Pipe">
</div>
<div class="form-group col-sm-2 cord">
<label for="header" style="overflow-wrap: normal;white-space: nowrap;">Is Header Present?</label>
<select class="form-control" id="opt" name="opt">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
</div>
<div class="form-row third" id="area2">
<div class="form-group col-sm-2 key" style="display:none;">
<label for="header" style="overflow-wrap: normal;white-space: nowrap;">comma separated header</label>
<input type="text" class="form-control" id="text9" name="text9" placeholder="Header Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Target Table Name</label>
<input type="text" class="form-control" id="text0" name="text0" placeholder="Hive Table Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Target Database Name</label>
<input type="text" class="form-control" id="text03" name="text03" placeholder="Database Name">
</div>
</div>
<button type="button" class="btn btn-primary btn-sm third">Cancel</button>
<input type="submit" formaction="/submit" id="submit" class="btn btn-primary btn-sm third" style="float:right; margin-right: 37%; display: none;" value="Save & Run" />
<div class="form-row sixth" id="area3" style="display:none;">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Line of Business</label>
<input type="text" class="form-control" id="lob" name="lob" placeholder="Insights & Data">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Distributed To</label>
<select class="form-control" id="distr" name="distr">
<option>EDL-WLDS</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4">Environment</label>
<select class="form-control" id="envir" name="envir">
<option>UAT</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
</div>
<div class="form-row sixth" id="area4" style="display:none;">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Select Tool</label>
<select class="form-control" id="fab" name="fab">
<!-- <option>Abinitio</option>
<option>Spark</option> -->
<option>Talend Data Fabric</option>
</select>
</div>
<div class="form-group col-sm-2 cord">
<label for="inputAddress" style="overflow-wrap: normal;white-space: nowrap;">Source Name</label>
<input type="text" class="form-control" id="srcn" name="srcn" placeholder="Source Name">
</div>
<!-- <div class="form-group col-sm-2 cord">
<label for="inputAddress">Line of Business</label>
<select class="form-control">
<option>Insights & Data</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div> -->
<div class="form-group col-sm-2 cord">
<label for="inputAddress" style="overflow-wrap: normal;white-space: nowrap;">Source Owner</label>
<input type="text" class="form-control" id="owner" name="owner" placeholder="Source Owner">
</div>
</div>
<div class="form-row fourth" id="area11" style="display:none;">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Input File Path</label>
<input type="text" class="form-control" id="text1" name="text1" placeholder="Path">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">File Name</label>
<input type="text" class="form-control" id="text2" name="text2" placeholder="File Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Delimiter</label>
<input type="text" class="form-control" id="text3" name="text3" placeholder="Delimiter">
</div>
</div>
<div class="form-row fourth" id="area11" style="display:none;">
<div class="form-group col-sm-2">
<label for="header" style="overflow-wrap: normal;white-space: nowrap;">Is Header Present?</label>
<select class="form-control" id="arc" name="arc">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
<div class="form-group col-sm-2 key cord" style="display:none;">
<label for="header" style="overflow-wrap: normal;white-space: nowrap;">comma separated header</label>
<input type="text" class="form-control" id="sep" name="sep" placeholder="Header Name">
</div>
<div class="form-group col-sm-2 cord">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Target Table name</label>
<input type="text" class="form-control" id="targetb" name="targetb" placeholder="Hive Name">
</div>
</div>
<div class="form-row fourth" id="area11" style="display:none;">
<div class="form-group col-sm-2">
<label for="inputEmail4" style="overflow-wrap: normal;white-space: nowrap;">Target Database name</label>
<input type="text" class="form-control" id="targetdb" name="targetdb" placeholder="Database Name">
</div>
</div>
<button type="button" class="btn btn-primary btn-sm fourth" style="display:none;">Cancel</button>
<input type="submit" id="submit1" formaction="/file" name="save_button" class="btn btn-primary btn-sm fourth" style="float:right; margin-right: 37%;" value="Save & Run" />
我正在尝试向数据库提交表单。这些值没有被提交,而是在数据库上我得到了未定义。基本上我的 req.body 是未定义的,不会传入值。
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var mysql = require('mysql');
var http = require('http');
//var formidable = require("express-formidable");
var path = require("path");
var shell = require('shelljs');
var formidable = require('formidable');
app.use(express.static('Project'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
var mysqlConnection = getConnection();
function getConnection() {
return mysql.createConnection({
host: 'localhost',
user: 'root',
port: '3306',
database: 'abc',
multipleStatements: true
});
}
app.post('/submit', (req, res) => {
console.log("entered");
console.log('line', req.body.line);
var form = new formidable.IncomingForm();
var application = req.body.namel;
console.log("appli", req.body.namel);
var useCase = req.body.mof;
console.log("value in use case", req.body.mof);
var line = req.body.line;
var accessKey = req.body.key;
var distributed = req.body.dist;
console.log("distributed", req.body.dist);
var environment = req.body.env;
console.log("envir", req.body.env);
var tool = req.body.data;
var sname = req.body.source;
console.log("source", req.body.source);
var owner = req.body.own;
var secretKey = req.body.secret;
var bucketName = req.body.bucket;
var fileName = req.body.fkey;
var delimiter = req.body.delim;
delimiter = '|';
var comma = req.body.text9;
var header = req.body.opt;
var tableName = req.body.text0;
console.log("fileName", fileName);
console.log("delimiter", delimiter);
console.log("header", header);
console.log("tableName", tableName);
var dbName = req.body.text03;
var nextVal;
var sql = 'select ifnull(max(id),0)+1 as nextJobId from talendfw';
var query = mysqlConnection.query(sql, (err, results) => {
if (err) throw err;
nextVal = results[0].nextJobId;
console.log(nextVal);
var queryString = "INSERT INTO talendfw (ID, Application_Name, t_key, t_value) VALUES('" + nextVal + "','" + useCase + "', 'line_of_business','" + line + "'), ('" + nextVal + "','" + useCase + "', 'distributed_to','" + distributed + "'), ('"+nextVal+"','" + useCase + "', 'environment','" + environment + "'),('"+nextVal+"','" + useCase + "', 'select_tool','" + tool + "'),('"+nextVal+"','" + useCase + "','source_name','" + sname + "'), ('"+nextVal+"','" + useCase + "', 'source_owner','" + owner + "'), ('"+nextVal+"','" + useCase + "', 'aws_access_key','" + accessKey + "'), ('"+nextVal+"','" + useCase + "', 'aws_secret_key','" + secretKey + "'), ('"+nextVal+"','" + useCase + "', 'aws_bucket_name','" + bucketName + "'), ('"+nextVal+"','" + useCase + "', 'key','" + fileName + "'), ('"+nextVal+"','" + useCase + "', 'delimiter','" + delimiter + "'), ('"+nextVal+"','" + useCase + "', 'is_header_present','" + header + "'), ('"+nextVal+"','" + useCase + "', 'comma_separated_header','" + comma + "'), ('"+nextVal+"','" + useCase + "', 'target_table_name','" + tableName + "'), ('"+nextVal+"','" + useCase + "', 'target_database_name','" + dbName + "')";
console.log("insert_query " + queryString);
mysqlConnection.query(queryString, (err, results, fields) => {
if (err) {
console.log("Failed to insert data" + err)
res.sendStatus(500);
return;
} else {
console.log("Inserted the values to DB successfully. ready for next operation", results);
shell_script(nextVal);
}
console.log("Operation completed");
});
请帮我解决这个问题。我使用了 express-formidable npm 而不是 req.body 我使用了 req.fields ,它工作正常但是与另一个使用强大的 npm 的表单数据有冲突。
您的表单元素具有此属性:
enctype="multipart/form-data"
这意味着您发送的请求的内容类型是multipart/form-data。如果删除它,表单将使用默认内容类型,即 x-www-form-urlencoded。 Bodyparser 应该能够解析它。
如果您仍想使用 multipart/form-data(您可以查看 this 关于什么更好的问题),您必须处理某种程度上的其他形式。
顺便说一下,您的代码可能对 SQL injection 来说很脆弱。在进行查询时,我会非常小心地使用字符串连接。您也不必再使用 body-parser 来解析正文。它已经合并到 Express 中,这意味着你可以做
app.use(express.urlencoded({ extended: false }));
app.use(express.json());