Express 入门
Express 介绍
<!-- views/index.html -->
<html>
<body>
<div>Hello World</div>
</body>
</html>
// server.js
// 1. 引入
var express = require("express");
var app = express();
// 静态资源: 图片等
app.use(express.static("dir"));
// 2. 设置路由
app.get("/", function(request, response) {
response.sendFile("views/index.html");
});
app.get("/:id", function(request, response) {
console.log("id:", request.params.id);
console.log("q:", request.query);
response.send("Hello");
});
// 3. 监听端口
var server = app.listen(8080, function() {
console.log("Server started");
});
Express 中间件
处理请求、响应
function middleware(request, response, next) {
// 操作request, response
next(); // 调用堆栈中下一个中间件
}
app.use(middleware, middleware);
app.get("/", middleware);
Express 路由
模式: controller/action
// router.js
// 路由中间件
var router = require("express").Router();
// 相关分组
router.get("/controller/action", [callback0, callback1]);
// 同路由简写
router
.get("/", fn0)
.post(fn1)
.put(fn2);
module.exports = router;
// server.js
var express = require("express");
var app = express();
app.use(require("./router.js"));
Express 错误处理
function middleware0(error, request, response, next) {
if (error) {
console.error(error.stack);
next(error);
}
}
function middleware1(error, request, response, next) {
if (!error) return next();
response.sendStatus(500);
}
app.use(middleware0, middleware1);
Express 模版引擎
引擎有 jade(现为 pug), swig, ejs…
<!-- views/index.html -->
<html>
<head>
<title></title>
</head>
<body></body>
</html>
// server.js
var swig = require("swig");
app.set("view engine", "html");
app.engine("html", swig.renderFile);
app.get("/", function(req, res) {
res.render("index", {
title: "title"
});
});