admin管理员组文章数量:1355731
I have set up a basic express server to accept some information submitted through a form. Unfortunately, I have run into some problems. I cannot log the data I receive onto the console. Can someone please help me figure this out?
app.js:
const express = require('express');
const path = require('path');
const app = express();
const port = 3000;
app.use(express.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname,'public')));
app.use('/',(req,res,next) => {
res.sendFile(path.join(__dirname,'public','index.html'));
});
app.post('/api', (req, res) => {
console.log(req);
})
app.listen(port, () => console.log(`App is listening on port ${port}!`))
And here's the form itself: index.html:
<body>
<h1>Hello there!</h1>
<form id='form' method="POST" action="/api">
<input id='textField' type='text' name='name' placeholder='Enter your name'>
<p>Enter your date of birth:</p>
<div class='dob'>
<input id='date' type='number' name='date' placeholder='Date'>
<select id='dobMonth' name="month">
<option value="default">Month</option>
<option value="Jan">January</option>
<option value="Feb">February</option>
<option value="Mar">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="Aug">August</option>
<option value="Sept">Septmeber</option>
<option value="Oct">October</option>
<option value="Nov">November</option>
<option value="Dec">December</option>
</select>
<input id='year' type='number' name='year' placeholder='Year'>
</div>
<input id='btn' type='submit'>
</form>
<script src='script.js'></script>
</body>
Thanks in advance :)
I have set up a basic express server to accept some information submitted through a form. Unfortunately, I have run into some problems. I cannot log the data I receive onto the console. Can someone please help me figure this out?
app.js:
const express = require('express');
const path = require('path');
const app = express();
const port = 3000;
app.use(express.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname,'public')));
app.use('/',(req,res,next) => {
res.sendFile(path.join(__dirname,'public','index.html'));
});
app.post('/api', (req, res) => {
console.log(req);
})
app.listen(port, () => console.log(`App is listening on port ${port}!`))
And here's the form itself: index.html:
<body>
<h1>Hello there!</h1>
<form id='form' method="POST" action="/api">
<input id='textField' type='text' name='name' placeholder='Enter your name'>
<p>Enter your date of birth:</p>
<div class='dob'>
<input id='date' type='number' name='date' placeholder='Date'>
<select id='dobMonth' name="month">
<option value="default">Month</option>
<option value="Jan">January</option>
<option value="Feb">February</option>
<option value="Mar">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="Aug">August</option>
<option value="Sept">Septmeber</option>
<option value="Oct">October</option>
<option value="Nov">November</option>
<option value="Dec">December</option>
</select>
<input id='year' type='number' name='year' placeholder='Year'>
</div>
<input id='btn' type='submit'>
</form>
<script src='script.js'></script>
</body>
Thanks in advance :)
Share Improve this question asked Jan 14, 2020 at 18:54 Debabrata MondalDebabrata Mondal 1011 gold badge4 silver badges13 bronze badges 2- Are you submitting the form normally or using AJAX? – Barmar Commented Jan 14, 2020 at 18:57
- The form is being submitted normally. I did try to use fetch API. But none of these worked successfully. – Debabrata Mondal Commented Jan 14, 2020 at 19:01
3 Answers
Reset to default 5The problem is just the order of your routes. The first path you specify:
app.use('/',(req,res,next) => {
res.sendFile(path.join(__dirname,'public','index.html'));
});
Is acting as a catch-all, since every path on the server includes '/'
If you switch the order and make the catch-all last, this should work just fine for you.
app.get('/api', (req, res) => {
console.log(req);
})
app.use('/',(req,res,next) => {
res.sendFile(path.join(__dirname,'public','index.html'));
});
I think for this you need body-parser middleware and tnen you can get parsed data from request:
here is simple example:
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
res.send('wele, ' + req.body.username)
})
// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
// create user in req.body
})
But it's does not handle multipart bodies (post with files). Due to their plex and typically large nature. For multipart bodies, you may be interested in the following modules:
busboy
formidable
multer
<form class="innerWidth" action="/contact-detail/" method="post">
<label for="">Name</label>
<input type="text" name="name">
<label for="">Business Email</label>
<input type="email" name="email" >
<label for="">Role</label>
<input type="text" name="role">
<label for="">Contact No.</label>
<input class="contactInput" type="text" name="number" >
<label for="">Message</label>
<input class="messageInput" type="text" name="message" >
<div class="submitBtn">
<input type="submit" value="Submit">
</div>
</form>
app.post('/contact-detail/', jsonParser, function(req, res) {
// create user in req.body
})
本文标签: javascriptHow to submit form data to express serverStack Overflow
版权声明:本文标题:javascript - How to submit form data to express server? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744049220a2582108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论