我是java脚本和node.js
的新手;如果出现一些错误。请原谅我。
我正在尝试编写一个简单的Node.Js
服务器端应用程序。
我想在这里实现的是。在视图上,我有文本框和提交按钮。当我通过提交按钮。文本框中写入的数据应传递给路由器(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
。
此致 拉杰什
答案 0 :(得分:0)
安装模块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);