开头先说写无关的话: 看了很多技术博客,最痛恨的就是那种拷贝粘贴党,要么就是那种原文翻译混乱党, 都是属于无脑型。
不仅如此,格式也是让人心生绝望。浪费了很多时间。
当然有些人写这些只是给自己记的笔记,自己也不能太过激。
好了,牢骚发过了,现在写正题。
express 4.x相比express 3.x有很大的改进。
特别是中间件方面,已经不再兼容,需要自己去下载。
所以只要碰到这种格式的 express.middleware, 在express 4.x都是用不了的。
以前的中间件logger模块,现在已经改名叫morgan了。
愚蠢的我也是踩了这些坑。
先安装模块
npm install morgan --save
morgan的用途就是记录http通信时的操作日志。
先上点代码吧。
这是服务器端, app.js:
var express = require('express');
var morgan = require('morgan');
var app = express();
app.use(morgan('combined'));//运用中间件,‘combined’是日志显示的格式,后面会看到什么样
app.get('/index2.html', function (req, res) {
res.sendFile(__dirname + '/index2.html');
}); //将本地文件夹中的index2.html文件传到客户端
app.post('/index2.html', function (req, res) {
req.on('data', function (data) {
console.log(data.toString());
res.end();
}); //响应index2.html的post请求
});
app.listen(1338);//监听localhost:1338端口
这是客户端html2.html文件:
通过ajax来post表单