Node.js & 快递表格提交 returns 404

Node.js & Express Form Submit returns 404

我正在尝试制作一个简单的文件上传系统,但每次我提交上传的文件时都会收到 404 错误。我尝试了各种不同的名称和路径,但它们都立即失败了。当我按下提交时,浏览器尝试重定向到 /upload 然后 return 出现 404 错误。我是 node.js 的新手,所以解决起来也很简单,但我找不到它。

我尝试了 2 个不同的模块,formidableexpress-fileupload。两者return同样的错误。

./views/test2.ejs

<html>
<head>
</head>
<body>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="filetoupload"><br>
        <input type="submit">
    </form>
</body>

./routes/test2.js

var express = require('express');
var router = express.Router();
var http = require('http');
var formidable = require('formidable');


router.get('/', function (req, res, next) {
    res.render('test2');
    res.sendFile(__dirname + '/test2.ejs');
});
router.post('/upload', function (req, res, next) {
    var form = new formidable.IncomingForm();

    form.parse(req);

    form.on('fileBegin', function (name, file) {
        file.path = __dirname + '/uploads/' + file.name;
    });

    form.on('file', function (name, file) {
        console.log('Uploaded ' + file.name);
    });

    res.sendFile(__dirname + '/test2.ejs');
});

module.exports = router;

./app.js

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var expressValidator = require('express-validator');
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');

mongoose.connect("REDACTED", function(err,db){
  if(!err){
    console.log("Connected");
  }
});

var index = require('./routes/index');
var users = require('./routes/users');
var login = require('./routes/login');
var form  = require('./routes/form');
var buttonrelay = require('./routes/buttonrelay');
var tesekkurler = require('./routes/tesekkurler');
var test = require('./routes/test');
var test2 = require('./routes/test2');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
//app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//app.use(expressLayout);
// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));
app.use(expressValidator());
// Express Session
app.use(session({
    secret: REDACTED,
    saveUninitialized: true,
    resave: true
}));

app.use('/', index);
app.use('/users', users);
app.use('/login', login);
app.use('/form', form);
app.use('/buttonrelay', buttonrelay);
app.use('/tesekkurler', tesekkurler);
app.use('/test', test);
app.use('/test2', test2);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

您使用 app.use('/test2', test2); 作为 /upload 路由
所以表单动作应该是 "/test2/upload"