gpt4 book ai didi

javascript - 如何将数据从表单传递到路由器

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:40 24 4
gpt4 key购买 nike

我是 java 脚本和 node.js 的新手;如果出现一些错误。请原谅我。

我正在尝试编写一个简单的 Node.Js 服务器端应用程序。

我在这里想要实现的是。在 View 上,我有文本框和提交按钮。当我通过提交按钮时。文本框中写入的数据应传递给 router(index.js) 并进行处理。

请参阅下面我的代码。

查看代码

extends layout

block content
h1= title
form#formReadCSV(name="readCSVFile",method="post",action="/readCSVFile")
input#inputCompleteCSVPath(type="text", placeholder="Complete Path" name="fileCSV")
button#btnOpenFile(type="submit") Submit


router(index.js)
<小时/>
router.post('/readCSVFile',function(req,res){

/*Read the complete Path*/
var fileName = req.body.fileCSV;

//console.log(fileName);

var reader = csv.createCSVFileReader(fileName,{
'separator':',',
'quote':'"',
'escape':'"',
});

reader.addListener('data',function(data){
console.log(data);
});

reader.addListener('end', function() {
console.log("END");
res.render('success_csv_upload', {message : "Successfully consumed the CSV file"});
});

现在,当按下提交按钮时。这是一些无法到达路由器的原因。我收到以下错误

csv is not defined
ReferenceError: csv is not defined
at /Users/rajesh/Documents/NodeProject/nodetest1/routes/index.js:26:15
at Layer.handle [as handle_request] (/Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/layer.js:82:5)
at /Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/index.js:234:24
at Function.proto.process_params (/Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/index.js:312:12)
at /Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/index.js:228:12
at Function.match_layer (/Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/index.js:295:3)
at next (/Users/rajesh/Documents/NodeProject/nodetest1/node_modules/express/lib/router/index.js:189:10)

有什么方法可以调试node.js

问候,拉杰什

最佳答案

安装模块 csv

npm install ya-csv

将其添加到您的代码

var csv = require('ya-csv');

示例:

模块

npm install ya-csv
npm install multer

表格

form#formReadCSV(name='readCSVFile', method='post', action='/readCSVFile', enctype='multipart/form-data')
input#inputCompleteCSVPath(type='file', placeholder='Complete Path', name='fileCSV')
button#btnOpenFile(type='submit') Submit

JS

var express = require('express');
var app = express();
var router = express.Router()
var csv = require('ya-csv');
var multer = require('multer');

app.set('view engine', 'jade');
app.set('views', __dirname + '/views');

app.use(multer({ dest: './uploads/'}))

router.get('/', function (req, res) {
res.render('index');
});

router.post('/readCSVFile', function (req, res) {
var reader = csv.createCsvFileReader(req.files.fileCSV.path, {'separator':',', 'quote':'"', 'escape':'"'});

reader.addListener('data', function (data) {
console.log(data);
});

reader.addListener('end', function() {
res.send('File was processed');
});
});

app.use('/', router);
app.listen(3000);

关于javascript - 如何将数据从表单传递到路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27034580/

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