学习节点并表达JS atm
我的应用服务器就是这样
require('./retrieveresults.js')();
require('./addresults.js')();
var express = require('express');
var path = require('path');
var app = express();
const PORT = 8080;
var bodyParser = require('body-parser');
//view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({extended : true}));
var router = express.Router();
// simple post Employee API --> sample inserting data --> use postman to execute
router.post('/addemployee', function (request, res) {
console.log(request.body)
var empid = request.body.empid;
var fname = request.body.fname;
var lname = request.body.lname;
var email = request.body.email;
var phonenumber = request.body.phonenumber;
var hire_date = request.body.hire_date
var job_id = request.body.job_id;
var salary = request.body.salary;
var comm_pt = request.body.comm_pt;
var manager = request.body.manager;
var department_id = request.body.department_id;
addDB("INSERT INTO employees VALUES (" + empid + "," + "'"+ fname + "'"+ "," + "'"+ lname + "'"+ "," + "'"+ email + "'"+ "," + "'"+ phonenumber + "'"+ "," + "TO_DATE("+ "'"+ hire_date + "'"+ "," + "'dd-MM-yyyy')," + "'"+ job_id + "'"+ "," + salary + ","+ comm_pt + "," + manager + "," + department_id + ")",
function(err,result){
if (!err){
console.log("success")
}else{
console.log(err);
}
})
});
app.use('/', router);
app.listen(PORT, function () {
console.log('Listening on port ' + PORT);
});
我的EJS模板是如下的正常形式:
<% include partials/header.ejs %>
<div class="container">
<form class="add-player-form" action="/addemployee" method="post" enctype="multipart/form-data">
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" name="first_name" id="first-name" placeholder="First Name" required>
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" name="last_name" id="last-name" placeholder="Last Name" required>
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" name="email" id="email" placeholder="email" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" name="number" id="number" placeholder="Phone Number" required>
</div>
<div class="form-group col-md-4">
<input type="date" class="form-control" name="date" id="date" placeholder="Date" required>
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" name="job" id="job" placeholder="job" required>
</div>
<div class="form-group col-md-4">
<select id="comm" name="position" class="form-control" required>
<option selected disabled>Choose comm PT</option>
<option>0.1</option>
<option>0.2</option>
<option>0.3</option>
<option>0.4</option>
</select>
</div>
<div class="form-group col-md-4">
<select id="manager" name="position" class="form-control" required>
<option selected disabled>Choose manager ID</option>
<% for (var row = 0; row < players.rows.length; row++) { %>
<option><%= players.rows[row][0]%></option>
<% } %>
</select>
</div>
<div class="form-group col-md-4">
<input type="number" class="form-control" name="salary" id="salary" placeholder="salary" required>
</div>
</div>
<div class = "form-row">
<div class="form-group col-md-4">
<select id="depart_id" name="position" class="form-control" required>
<option selected disabled>Choose dept ID</option>
<% for (var row = 0; row < players.rows.length; row++) { %>
<option><%= players.rows[row][1]%></option>
<% } %>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary float-right">Add Employee</button>
</form>
</div>
</div>
</body>
</html>
我正在尝试从请求正文中的表单中提取值,但是当我执行控制台日志时,它返回了{}。我很确定我已经正确使用了正文解析。
我在做什么错了?
ps:对ejs和node js仍然是新手
答案 0 :(得分:0)
您使用的是enctype="multipart/form-data"
,因此表单提交的内容类型为multipart / form-data,通常用于上传文件。但是我看不到上面表单中输入的任何文件类型。
因此,请尝试从表单中删除enctype="multipart/form-data"
。默认情况下,它将使用url-encoded
作为格式。
答案 1 :(得分:0)
我在查看您使用 enctype="multipart/form-data"
的代码时看到的情况。
如果您不必上传任何图像,请尝试删除此代码。 multipart/form-data 一般用于上传文件或图片。
我已经删除了您的 multupart/formdata 并将其设为简单表单,如果您想上传文件,您可以根据您的要求使用 multer 或 skipper S3。
<% include partials/header.ejs %>
<div class="container">
<form class="add-player-form" action="/addemployee" method="post">
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" name="first_name" id="first-name" placeholder="First Name" required>
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" name="last_name" id="last-name" placeholder="Last Name" required>
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" name="email" id="email" placeholder="email" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<input type="text" class="form-control" name="number" id="number" placeholder="Phone Number" required>
</div>
<div class="form-group col-md-4">
<input type="date" class="form-control" name="date" id="date" placeholder="Date" required>
</div>
<div class="form-group col-md-4">
<input type="text" class="form-control" name="job" id="job" placeholder="job" required>
</div>
<div class="form-group col-md-4">
<select id="comm" name="position" class="form-control" required>
<option selected disabled>Choose comm PT</option>
<option>0.1</option>
<option>0.2</option>
<option>0.3</option>
<option>0.4</option>
</select>
</div>
<div class="form-group col-md-4">
<select id="manager" name="position" class="form-control" required>
<option selected disabled>Choose manager ID</option>
<% for (var row = 0; row < players.rows.length; row++) { %>
<option><%= players.rows[row][0]%></option>
<% } %>
</select>
</div>
<div class="form-group col-md-4">
<input type="number" class="form-control" name="salary" id="salary" placeholder="salary" required>
</div>
</div>
<div class = "form-row">
<div class="form-group col-md-4">
<select id="depart_id" name="position" class="form-control" required>
<option selected disabled>Choose dept ID</option>
<% for (var row = 0; row < players.rows.length; row++) { %>
<option><%= players.rows[row][1]%></option>
<% } %>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary float-right">Add Employee</button>
</form>
</div>
</div>
</body>
</html>