gpt4 book ai didi

javascript - 如何在运行 Node.js 的服务器上保存文件?

转载 作者:行者123 更新时间:2023-11-30 15:28:59 25 4
gpt4 key购买 nike

我有一台运行 Node.js 的服务器。我必须从 html 页面(“index.html”)读取表单数据并将该数据写入 txt 文件。

这是我的 index.html:

<form action="/uploads" method="post">
<table>
<tr>
<td><img src="images/ktulogo.png" width="100" height="100"></td>
<td>
<h3>Karadeniz Teknik Üniversitesi Bilgisayar Mühendisliği Bölümü</h3>
</td>
</tr>
<tr>
<td><br></td>
<td>
<hr>
</td>
</tr>
<tr>
<td>
<h4>Adınız Soyadınız :</h4>
</td>
<td><input type="text" style="margin-left: 5%; width: 40%"></td>
</tr>
<tr>
<td>
<h4>Öğrenci Numaranız :</h4>
</td>
<td><input name="ogrNo" type="text" style="margin-left: 5%; width: 40%"></td>
</tr>
<tr>
<td>
<h4>Cevabınız :</h4>
</td>
<td><textarea name="answer" datatype="text" rows="10" cols="100" style="margin-left: 5%; resize: none"></textarea></td>
</tr>
</table>
</br>
<center>
<button id="saveFile" class="btn btn-success" style="width: 30%">Sisteme Yükle</button>
</center>
</form>

这是我的 app.js:

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

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'views/index.html'));
});

app.post('/uploads', function (req, res) {
var file_name = req.body.ogrNo;
var file_content = req.body.answer;
file_content = file_content.replace(/\n/g, "\r\n");

var stream = fs.createWriteStream(file_name+".txt");
stream.once('open', function () {
stream.write(file_content);
stream.end();
});
});

var server = app.listen(3000, function(){
console.log('Server listening on port 3000');
});

当我运行它时,它抛出一个错误:

TypeError: Cannot read property 'ogrNo' of undefined at c:\Users\Volkan\IdeaProjects\File Upload\app.js:13:27 at Layer.handle [as handle_request] (c:\Users\Volkan\IdeaProjects\File Upload\node_modules\express\lib\router\layer.js:95:5) at next (c:\Users\Volkan\IdeaProjects\File Upload\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (c:\Users\Volkan\IdeaProjects\File Upload\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (c:\Users\Volkan\IdeaProjects\File Upload\node_modules\express\lib\router\layer.js:95:5) at c:\Users\Volkan\IdeaProjects\File Upload\node_modules\express\lib\router\index.js:277:22 at Function.process_params (c:\Users\Volkan\IdeaProjects\File Upload\node_modules\express\lib\router\index.js:330:12) at next (c:\Users\Volkan\IdeaProjects\File Upload\node_modules\express\lib\router\index.js:271:10) at serveStatic (c:\Users\Volkan\IdeaProjects\File Upload\node_modules\serve-static\index.js:75:16) at Layer.handle [as handle_request] (c:\Users\Volkan\IdeaProjects\File Upload\node_modules\express\lib\router\layer.js:95:5)

那么,我的问题是什么原因造成的,或者我该如何处理这份工作?

最佳答案

您必须解析传入的数据,为此使用 body-parser

首先安装它

npm i -S body-parser

然后将其导入到您的代码中

var bodyParser = require('body-parser')

添加解析器

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));

// parse application/json
app.use(bodyParser.json());

就这些。

关于javascript - 如何在运行 Node.js 的服务器上保存文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42526411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com