无法发布错误 - node.js - 使用multer

时间:2016-06-08 17:00:08

标签: javascript html node.js multer

index.html通过点击主页“localhost:9000 /”中名为“spons”的链接到达浏览器,并且网址更改为“localhost:9000 / spons”,现在当我下载表单时,它会进入浏览器到网址“localhost:9000 / spons / uploads”并给出“不能发布/赞助/上传”,images文件夹与server.js文件在同一目录下,请你帮我解决一下这个问题?

这是index.html文件

<form name='uploadform' enctype='multipart/form-data' method='post' action='/spons/uploads'>
<input name='imageupload' type='file'>
<input type="submit" value="Upload Image" name="submit">
</form>
这是server.js文件

var express = require('express');
var multer= require('multer');

var upload= multer({dest:'/images'}).single('imageupload');


var router = express.Router();
router.post('/spons/uploads', upload, function(req,res){
  res.end('uploaded');
   }
 );

express().listen(9000);

2 个答案:

答案 0 :(得分:0)

您收到cannot POST /spons/uploads,因为您没有将router附加到该应用。

var express = require('express');
var app = express();
var multer= require('multer');

var upload= multer({dest:'/images'}).single('imageupload');

var router = express.Router();
router.post('/spons/uploads', upload, function(req,res){
   res.end('uploaded');
});

app.use('/',router); // this line is the key. 

app.listen(9000);

希望,它有所帮助。

答案 1 :(得分:0)

您甚至不需要显式路由器,请查看下面的示例

var express = require('express');
var app = express();
var multer= require('multer');

var upload= multer({dest:'/images'}).single('imageupload');

app.post('/spons/uploads', upload, function(req,res){
   res.end('uploaded');
});

app.listen(9000);