【问题标题】:Get user input from html form and write it to text, csv or other database file (express.js, node.js, html)从 html 表单获取用户输入并将其写入文本、csv 或其他数据库文件(express.js、node.js、html)
【发布时间】:2020-03-08 21:38:49
【问题描述】:

我有一个网站,用户可以在该网站上请求他们想在哪辆车上查看文章。我想将输入保存到服务器端文件(不保存到用户的计算机),以便我可以看到用户想要阅读的内容。

服务器代码(express.js、node.js):

var express = require('express');
var app = express();

app.set('trust proxy', true)

//setting middleware
app.use(express.static('/', {index:"/seriousindex.html"})); //Serves resources from public folder
console.log(__dirname);

app.use(express.static(__dirname+"/resources/css"));
app.use(express.static(__dirname+"/resources/js"));
app.use(express.static(__dirname+"/resources/audio"));
app.use(express.static(__dirname+"/resources/visual"));

app.get('/', (req, res)=> {
    res.sendFile('/seriousindex.html', {root:__dirname});
});

app.get('/*', function (req, res) {
    console.log(req.originalUrl);
    console.log(req.ip.split(':').pop());
    res.sendFile(__dirname + req.originalUrl.replace("?", ""));
});

PORT = 9001
app.listen(PORT, () => {
 console.log(`Server is listening on port: ${PORT}`);
})

输入表单 (HTML):

<label>
    Enter a car you want to see info on here: 
    <form>
        <input type = "form" id = "requestinput" placeholder = "Write here"></input>
        <input type = "submit" method = "POST" onclick = "/submit"></input>
    </form>
</label>    

我更喜欢使用 node.js 或 HTML 而不是 php,但如果 php 脚本有效,我可能仍会使用它。 我从 StackOverflow 复制了很多代码,所以预计我的代码中会出现不一致的情况。 感谢您的帮助。

【问题讨论】:

    标签: javascript html node.js csv express


    【解决方案1】:

    已更新!您将不得不添加一个名为 bodyParser 的附加模块,它是一个帮助您解析传入请求正文的中间件。对不起,我才意识到!

    ...express()
    
    var bodyParser = require('body-parser')
    
    app.use(bodyParser.urlencoded({ extended: true }))
    app.use(bodyParser.json())
    
    app.post('/car', (req, res) => {
    ...
    

    在输入表单的元素中,您需要指定属性“action”。 已更新! 此外,您必须在字段中添加“名称”属性,以便将数据标记为像这样的键,例如 { "requestinput": "honda" }

    <label>
        Enter a car you want to see info on here: 
        <form action="/car" method="POST"> 
            <input type ="text" name="requestinput" placeholder="Write here"></input>
            <input type = "submit"></input>
        <!-- This onclick is used to call a function inside the <script> of the html. In your case, the html has no function inside <script></script> so it does nothing -->
        </form>
    <label>
    

    那么您的应用将需要一个名为“/car”的路线来接收输入

    app.post('/car', (req, res)=>{
        console.log('Data', req.body.requestinput) // changed to body
        // save the data here
    })
    

    【讨论】:

    • 感谢您的代码!它几乎可以工作,唯一的问题是当我提交某些内容时,它会显示“数据未定义”而不是“数据(我提交的内容)”。你知道有什么解决办法吗?另外:如果我做了&lt;form action="/car" method = "POST"&gt;,我只能让表单工作。
    • 您好,很抱歉让您感到困惑。我会改进提供的答案。由于 method="POST" 在 POST 正文中发送数据,您必须使用 "console.log('Data', req.body.requestinput)。此外,您的应用程序还需要使用 bodyParser 模块,该模块提供中间件来帮助解析 POST 正文中的数据。
    • 再一次,您的建议奏效了,但这次我还必须将“id”更改为“name”。谢谢。
    • 实际上@SyedOmarNoor,除非您需要访问输入元素,否则实际上并不需要 id。要发送的请求需要名称: { '' : ''} 。尽管如此,它现在工作很好!
    猜你喜欢
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 2018-11-03
    • 1970-01-01
    • 2018-11-08
    相关资源
    最近更新 更多