错误:请求失败,状态代码为404(Node.js + Vue.js)?

时间:2019-01-25 17:05:45

标签: javascript node.js vue.js

我有点困惑,需要一些帮助。

我使用 Node.js 编写HTTP服务器,并从 Vue.js 向HTTP服务器发出HTTP请求。它总是以某种方式返回错误:

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)

URL地址似乎不正确,导致浏览器中的错误为404。我多次检查了网址,但没有发现错误。我想念什么?

P.S。的主要任务是从网站从远程sftp服务器加载文件。我习惯使用该任务ssh2-sftp-client库作为后端。

当用户单击按钮时,应用程序运行getFile函数,我们将向HTTP服务器发送发布请求。

Vue.js 组件中的代码:

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)
    }
  })
}

2 个答案:

答案 0 :(得分:0)

您的const Module = require('module') const escapedPatchedModulePath = require.resolve('original-package/module-to-patch.js') .replace(/\\/g, '\\\\'); Module.wrapper[0] += ` const isPatchedModule = __filename === "${escapedPatchedModulePath}"; let process = isPatchedModule ? Object.create(global.process, { platform: { value: 'linux' } }) : global.process; `; require('original-package/bin/entry-point.js'); 文件中似乎缺少app.listen(port)

app.js

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

答案 1 :(得分:0)

controllers/csv.js中,您从不发送响应。您应该在某处放置res.sendres.renderres.json