NodeJS Express Route无法与表单操作一起使用

时间:2020-09-30 14:21:38

标签: javascript html node.js express

我有一个表单应该提交数据,以便节点js可以处理它。我遵循了确切的示例,但是由于某种原因,提交表单时始终出现以下错误:404:ENOENT:没有这样的文件或目录,状态为'c:\ path \ submit'

不确定我在哪里出错了,但是我有app.js在后台运行,并且在提交表单时,它永远不会传递到app.post('/ submit')

app.js

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

const port = 3335;

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});
   

//Receive data from form submission
app.use(bodyParser.urlencoded({ extended: true }));
let country, state, city, language = "";
app.post('/submit', (req, res) => {
    country = res.req.body.country;
    state = res.req.body.state;
    city = res.req.body.city;
    language = res.req.body.language;
    console.log(`'${country}','${state}','${city}','${language}'`)
    
    res.redirect("http://localhost:52330/success.html")
});

这是我的html表单:

        <form action="/submit" method="POST">
            <div class="banner">
            </div>
            
            <div class="position-item">
                <div class="item">
                    <p>What country are you from?</p>
                    <select name="country" required>
                        <option value="Afganistan">Afghanistan</option>
                        <option value="Albania">Albania</option>
                    </select>
                </div>
            </div>
            <div class="position-item">
                <div class="item">
                    <p>What state/province are you from?</p>
                    <select name="state" required>
                        <option value="Ontario">Ontario</option>
                        <option value="New York">New York</option>
                    </select>
                </div>
            </div>
            <div class="position-item">
                <div class="item">
                    <p>What town/city are you from?</p>
                    <select name="city" required>
                        <option value="Toronto">Toronto</option>
                        <option value="Mexico City">Mexico City</option>
                    </select>
                </div>
            </div>
            <div class="position-item">
                <div class="item">
                    <p>What is your native language?</p>
                    <select name="language" required>
                        <option value="English">English</option>
                        <option value="Spanish">Spanish</option>
                    </select>
                </div>
            </div>
    
            <div class="btn-block">
                <button type="submit">Submit</button>
            </div>
        </form>

0 个答案:

没有答案