简体   繁体   English

无法使用Node&Express(Jade,MongoDB,Express,Node)进行POST

[英]Cannot POST using node & Express (Jade, MongoDB, Express, Node)

I have a form in which a user can select a .xlsx file and upload it: 我有一个表单,用户可以在其中选择.xlsx文件并上传:

      p Upload New Schedule
      #uploadNew
        form(id = "form1", action="/uploadNew", method="post", enctype="multipart/form-data")
          input(type="file", id="control", name="XLupload")
          br
          input(type="submit" value="Upload" name="Submit")

in my main app.js I have this route to deal with it. 在我的主要app.js中,我有这条路线来处理它。

var uploads = require('./routes/upload');
//Make the db accessible to various http requests
app.use(function(req, res, next) {
  req.db = dbMain;
  next();
});
app.use('/', routes);
app.use('/upload', uploads);

and In my upload.js file I have the following: 在我的upload.js文件中,我具有以下内容:

var xlsx = require('xlsx');
var multer = require('multer');
var upload = multer({dest: './uploads'});
var excel_upload = upload.single('XLupload');

router.get('/upload', stormpath.groupsRequired(['Enforced']), function(req, res) {
    res.render('upload', {
        title: 'Uploading Excel File'
    });
    next();
});

router.post('/upload', excel_upload, function(req, res) {
    // Get the path to the uploaded Excel file
    var fileObject = req.file;
    var filePath = fileObject.path;

    // Get collection instance which will store the data from the newly posted schedule
    var dbLocal = req.db;
    var insertionCollection = dbLocal.collection('chip');
    .....

However, when I start up and listen at localhost, when I select a file and try to upload I get error cannot POST /upload. 但是,当我启动并在本地主机上侦听时,当我选择一个文件并尝试上传时,出现错误,无法开机自检/上载。 When I did not have routes and had everything in the main app.js file, this was not an issue. 当我没有路线并且所有内容都在主app.js文件中时,这不是问题。 What did I mess up by introducing routes? 通过介绍路线我搞砸了什么?

This: 这个:

app.use('/upload', uploads);

Combined with this: 与此结合:

router.post('/upload', ...)

Declares this route: POST /upload/upload 声明此路由: POST /upload/upload

If you mount a router (using app.use(PATH, router) ), PATH adds a prefix to all the routes handled by that router. 如果安装了路由器(使用app.use(PATH, router) ),则PATH会为该路由器处理的所有路由添加前缀。 Your router need to declare routes relative to that prefix: 您的路由器需要声明相对于该前缀的路由:

router.post('/', ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM