使用multer上传文件:posr route不会触发

时间:2016-08-15 09:56:48

标签: javascript node.js multer

我尝试使用multer来存储本地文件。我只是想运行示例,但我的app.post路由没有被触发。

这是我的简单代码(app.js)

    var express = require('express');
    var path = require('path');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');
    var multer  = require('multer');
    var upload = multer({ dest: 'uploads/' });

    var app = express();

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

    // 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(express.static(path.join(__dirname, 'public')));

    app.get("/", function(req, res){
      res.render("index", { title: 'Express' });
    });

    app.get("/upload", function(req, res) {
      res.render("upload");
    });


    app.post('/upload', upload.single('upl'), function (req, res) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
  console.log(req.file);
})
    ...// regular code generated by express

我的jade视图是ad follow(upload.jade)

extends layout
block content
  h1= title
  p Welcome to upload form
    form(method="post",enctype="multipart/form-data",action="/upload")
    p
      input(type="text",name="title", placeholder="title")
    p
      input(type="file",name="upl")
    p
      input(type="submit")

我有些遗漏但却看不到的东西。 THX

1 个答案:

答案 0 :(得分:0)

好的,很抱歉提出这个问题。 我专注于js文件,其中我的玉文件中的缩进不正确。形式是空的。所有的p必须在表格内。