解析节点js上的文件

时间:2016-06-16 15:03:18

标签: html node.js parsing express

我希望能够导入文本文件,然后下一页将吐出由逗号分隔的文件内容。我的代码如下。现在它不起作用。我希望能够读入导入的文件,然后打印出网页上的内容。我不知道如何使用强大的,也许这是一个选项?

节点js

 var http = require('http');
var express= require('express')
var fs = require('fs');
var util = require('util');
var app= express();
var multer = require('multer');
var fileUpload = require('express-fileupload');
app.use(fileUpload());

app.get('/', function(req, res){
    var html = fs.readFileSync('index2.html');
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(html);
});

var upload = multer({ dest: 'uploads/' })

app.post('/submit', upload.single('file'), function(req,res,next) {
    res.send(req.file);
});

//Listening to computer's IP address
app.listen(3000, "192.168.0.172");
console.log('Listening at 192.168.0.172:3000')

HTML

 <html>
  <head>
    <title>
      File Upload Example
    </title>
  </head>
  <body>
    <form action="/submit" method="post" enctype='multipart/form-data'>
    <br/>
    <input type="file" name="file" />
    <input type="submit" value="Submit" name="submit">
    </form>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

要在请求字段中访问该文件,您应该添加&#34; multer&#34;中间件到处理程序。 试试这个:

multer({ dest: 'uploads/' }); app.post('/submit', upload.single('Parameters'), function(req,res) { console.log(req.file); });