gpt4 book ai didi

node.js - Express 3.0 bodyParser 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 22:48:05 27 4
gpt4 key购买 nike

我正在使用最新版本的express no. 3.我已阅读文档,并且完全按照所写的操作进行操作,但它仍然无法按预期工作。提交后,我在上传目录中收到一个文件,但随后一切都停止了,并且 app.post 中的回调函数不会触发。代码:

HTML-JADE:

form(action="/upload", method="post", enctype="multipart/form-data")
input(type="file", name="image")
input(type='submit', value='submit')

应用程序.js:

var express = require('express')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, mongo = require('mongodb')
, Server = mongo.Server
, Db = mongo.Db
, routes = require('./routes')

var app = express();

app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser({uploadDir:'./upload'}));
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
});

app.post('/upload', function(req, res) {
console.log(req.files.image) // this doesn't fire at all - no matter what i write here
res.send(200) //doesn't run also
});

最佳答案

读取数据后需要返回响应。如果不返回响应,express 不知道您的响应何时完成, Node 也不会关闭与客户端的连接。

试试这个:

app.post('/upload', function(req, res) {
console.log(req.files.image);

req.on('data', function(raw) {
console.log('received data');
});

req.on('end', function() {
console.log('end');
res.send(200);
});
}

关于node.js - Express 3.0 bodyParser 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com