NodeJs的express模块4.X

一:express 4.x版本

express 4.x版本中已经将bodyParser中间件分离了,需要单独安装模块,就可以对post进行解析了。貌似默认依赖了bodyParser ,可以用来解析常规的表单提交。

但是文件上传的话需要另外安装模块官方推介的模块:connect-multiparty

github地址:https://github.com/andrewrk/connect-multiparty

示例:

在app.js 中使用var xxx = require();  

var express = require(‘express’);

var bodyParser = require(‘body-parser’);

var app = express();// use body parser so we can grab information from POST requests

app.use(bodyParser.urlencoded({extended: true})); 

//原先是这么写的在app.js 中使用var xxx = require();  
app.use(express.bodyParser());

app.post(’/student’,function(req,res){

console.log(req.body);

});

引入处理模块app.post(’/xx’,xxx);  路由控制将下面的内容放在你具体解析,

操作post的模块中var multipart = require(‘connect-multiparty’); //在处理模块中引入第三方解析模块

var multipartMiddleware = multipart();

app.post(’/upload’, multipartMiddleware, function(req, resp) {

console.log(req.body, req.files);/

/ don’t forget to delete all req.files when done

});

二:gitHub关于中间件的使用 https://github.com/senchalabs/connect#middleware

Connect is an extensible HTTP server framework for node using "plugins" known as middleware.

var connect = require('connect');
var http = require('http');

var app = connect();

// gzip/deflate outgoing responses
var compression = require('compression');
app.use(compression());

// store session state in browser cookie
var cookieSession = require('cookie-session');
app.use(cookieSession({
    keys: ['secret1', 'secret2']
}));

// parse urlencoded request bodies into req.body
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());

// respond to all requests
app.use(function(req, res){
  res.end('Hello from Connect!\n');
});

//create node.js http server and listen on port
http.createServer(app).listen(3000);

在app.js 中使用var xxx = require();  

你可能感兴趣的:(中间件,表单,nodejs,express)