简介
Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具。
使用 Express 可以快速地搭建一个完整功能的网站。
Express 框架核心特性:
- 可以设置中间件来响应 HTTP 请求。
- 定义了路由表用于执行不同的 HTTP 请求动作。
- 可以通过向模板传递参数来动态渲染 HTML 页面
实例
先来个helloworld简单实例
先新建01-express
文件夹 安装express
并将其保存到依赖列表中
cnpm install express --save
再安装几个重要模块
cnpm install body-parser --save
cnpm install cookie-parser --save
cnpm install multer --save
body-parser
Nodejs进阶:Express常用中间件body-parser实现解析
看下 express 使用的版本号
cnpm list express
代码
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World');
});
app.listen(3000, function () {
console.log('app is listening at port 3000');
});
用express
vs 原始代码
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('this is the homepage');
});
app.get('/contact', function (req, res) {
res.send('this is the contact page');
});
app.listen(3000, function () {
console.log('app is listening at port 3000');
});
//get: app.get('route',fn)
//post: app.post('route',fn)
//delete: app.delete('route',fn)
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
console.log('request was made: ' + req.url);
if (req.url === '/home' || req.url === '/'){
res.writeHead(200,{'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/index.html','utf8').pipe(res);
}else if(req.url === '/contact'){
res.writeHead(200,{'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/contact.html','utf8').pipe(res);
}else if(req.url === '/api/users'){
var users = [{name: 'AlexZ33', age:26}, {name: 'jingxin', age: 8}];
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(users));
}else {
res.writeHead(404,{'Content-Type': 'text/html'});
fs.createReadStream(__dirname + '/404.html','utf8').pipe(res);
}
});
server.listen(3000,'127.0.0.1');
console.log('now listening to port 3000');
上面一种是不是简单太多。
完整的项目开发过程
http://y.dobit.top/Detail/150...
常用中间件和开发包:
multer: Multer is a node.js middleware for handling multipart/form-data, which is primarily used for uploading files
morgan: HTTP request logger middleware for node.js
body-parser: Node.js body parsing middleware.
Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
pm2: 进程管理的,一般用于生产环境
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
rotating-file-stream: Creates a stream.Writable to a file which is rotated. Rotation behaviour can be deeply customized; optionally, classical UNIX logrotate behaviour can be used.
which: Like the unix which utility.
Finds the first instance of a specified executable in the PATH environment variable. Does not cache the results, so hash -r is not needed when the PATH changes.
chalk: Terminal string styling done right
logger
var logger = require('morgan');
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。