簡體   English   中英

無法使用Node&Express(Jade,MongoDB,Express,Node)進行POST

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

我有一個表單,用戶可以在其中選擇.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")

在我的主要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);

在我的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');
    .....

但是,當我啟動並在本地主機上偵聽時,當我選擇一個文件並嘗試上傳時,出現錯誤,無法開機自檢/上載。 當我沒有路線並且所有內容都在主app.js文件中時,這不是問題。 通過介紹路線我搞砸了什么?

這個:

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

與此結合:

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

聲明此路由: POST /upload/upload

如果安裝了路由器(使用app.use(PATH, router) ),則PATH會為該路由器處理的所有路由添加前綴。 您的路由器需要聲明相對於該前綴的路由:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM