Monday 8 May 2017

simple node file-uploading app isn't working

I'm trying to create my own node app. It should upload xml files to server, then on button click all xmls compiles into one string, which goes through regular expression. Matched results should be send to a client and represented as a list. I have some troubles in routing and in code organisation as well. Can you advise how to organise code.

It's my server file.

const express = require("express");
const pug = require('pug');
const fileLoader = require('./fileLoader');
const readFiles = require('./readFiles.js');
const regexpFilter = require('./regexpFilter.js');
const path = require('path');

const app = express();

let allContents = readFiles.allContents;
let compiledFunction = pug.compileFile('views/index.pug');


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

app.get('/', function(req, res) {
  res.send(compiledFunction({
    list: regexpFilter.mails(allContents)
  }));
});

app.post('/api/xml', function(req, res) {
  fileLoader.upload(req, res, function(err) {
    if (err) {
      return res.end("Error uploading file.");
    }
    res.end(
      compiledFunction({
        list: regexpFilter.mails(allContents)
      }));
    readFiles.filesToOne();
  });
});

app.listen(3000);

I organised files this way



via IHEWAZ

No comments:

Post a Comment