1

I am little bit confused and need some help.

I write an HTTP server using Node.js, and make an HTTP request from Vue.js to the HTTP server. Somehow it always return error like this:

Error: Request failed with status code 404
    at FtD3.t.exports (createError.js:16)
    at t.exports (settle.js:18)
    at XMLHttpRequest.f.(:3010/anonymous function) (http://localhost:3010/static/js/vendor.1dc24385e2ad03071ff8.js:1312:88758)

It seems like url address don't correct cause error is 404 in browser. I check url address several times but did't notice something wrong. What I miss?

P.S. The main task to load file from remote sftp server from website. I use to that task ssh2-sftp-client library as backend side.

When user click the button, application run getFile function where we send post request to HTTP server.

Code inside Vue.js component:

getFile (fileName) {
    axios.post('http://localhost:3010/csv', {file_name: fileName}, {headers: {'Authorization': this.token}}).then(response => {
        console.log(response)
        this.showAlert('You download file successfully.', 'is-success', 'is-top')
    }).catch((error) => {
        console.log(error)
        this.showAlert(error, 'is-danger', 'is-bottom')
    })
}

app.js:

const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
const path = require('path');
const bodyParser = require('body-parser');

const csvRouter = require('./server/routes/csv')

const app = express();

app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'dist')));
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());

app.use('/csv', csvRouter);

module.exports = app;

routers/csv.js:

const express = require('express')
const router = express.Router()

const csvControllers = require('../controllers/csv')

router.get('/', csvControllers.getFile)

module.exports = router

controllers/csv.js:

const request = require('request')
const queryString = require('query-string')
let Client = require('ssh2-sftp-client')
let sftp = new Client()

const config = require('../config')

exports.getFile = (req, res) => {
  console.log(req)  // In console I don't notice nothing.
  let data = {
    file_name: req.query.file_name
  }
  let options = {
    method: 'port',
    json: true,
    header: {'Authorization': req.header.token},
    url: `http://localhost:3010/csv?` + queryString.stringify(data)
  }
  request(options, (error, response) => {
    console.log('Message')  // In console I don't notice nothing.
    if (response) {
      sftp.connect(config.sftpServer).then(() => {
        return sftp.get('/reports/' + data.file_name)
      }).then((chunk) => {
        console.log(chunk)
      }).catch((err) => {
        console.log(err)
      })
    } else {
      response.status(500).send(error)
    }
  })
}
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193

2 Answers2

-1

It seems that app.listen(port) is missing in your app.js file:

app.listen(3000)

https://expressjs.com/en/starter/hello-world.html

Beniamin H
  • 2,048
  • 1
  • 11
  • 17
  • No, I don't miss it. It just in other file. Inside `bin/www`. I use such code inside that file: `let port = normalizePort(process.env.PORT || '3010')`, `app.set('port', port)`, then `const server = http.createServer(app)`. Finally `server.listen(port)` Do you have any ideas? – Nurzhan Nogerbek Jan 26 '19 at 05:49
  • Bro, what would be better? Use `server.lister(port)` or `app.lister(port)`? – Nurzhan Nogerbek Jan 26 '19 at 09:36
  • [app.listen vs server.listen](https://stackoverflow.com/questions/17696801/express-js-app-listen-vs-server-listen) – Beniamin H Jan 26 '19 at 14:57
  • I have no other clue - everything else seems to be fine. – Beniamin H Jan 26 '19 at 14:59
-1

In controllers/csv.js you never send a response. You should have a res.send or res.render or res.json somewhere.

Alexander
  • 638
  • 3
  • 11