在coffeescript中将multer与express结合使用时出现404错误

时间:2018-08-16 13:01:25

标签: node.js express coffeescript multer

我在进行这项工作时遇到了麻烦。我正在尝试从vuejs前端上传单个文件,以表示服务器后端。

该文件转到uploads文件夹,但未调用该函数本身。我只收到404响应。

我的文件。咖啡

express = require 'express'
router = express.Router()
fs = require 'fs'
config = require '../config.coffee'
multer = require 'multer'
upload = multer {dest: 'uploads/'}

router.use upload.single 'file'
router.post '/uploadfile', (req, res) ->
  console.log req.file
  res.send 'This never gets to be sent'

module.exports = router

我的server.coffee

path = require 'path'
http = require 'http'
express = require 'express'
bodyParser = require 'body-parser'
cookieParser = require 'cookie-parser'
coffeescript = require 'connect-coffee-script'
cors = require('cors')
cron = require 'node-cron'
axios = require 'axios'
cluster = require('cluster');
numCPUs = require('os').cpus().length


# -> Custom routes
files = require './routes/files.coffee'

app = express()
server = http.createServer app

app.use cors()
app.use bodyParser.json()
app.use bodyParser.urlencoded extended: false
app.use cookieParser()

if cluster.isMaster
  for name in [1..numCPUs]
    cluster.fork();

  cluster.on 'exit', (worker,code,signal) =>
    console.log "#{worker} died"
else
  app.use coffeescript
    src: path.join(__dirname, 'app')
    dest: path.join(__dirname, 'public/js')
    prefix: '/js'
    compress: true
    bare: true

  app.use express.static path.join(__dirname, 'public')
  # register custom routes
  app.use '/files', files

  app.use (req, res, next) ->
    err = new Error 'Not Found'
    err.status = 404
    next err

  app.use (err, req, res, next) ->
    res.status err.status || 500
    res.send err.message

  Port = process.env.PORT || 4001
  server.listen Port
  console.log("Process id #{process.pid} is at http://localhost:#{Port}")

module.exports = app

所以我已经尝试注释掉express.static路径,因为我知道这会引起一些问题。

有人知道我缺少什么吗? 因此,我一直在multers github页面上,并且有很多原因导致它出现404错误,但似乎所有修复都不对我有用。 预先感谢

0 个答案:

没有答案