-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
92 lines (75 loc) · 2.08 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Created by yataozhang on 8/22/16.
*/
var path = require('path');
var express = require('./express');
// 引入第三方请求主体格式化工具
var bodyParser = require('./thirdMiddleware/body-parser');
// 引入第三方模版引擎
var ntl = require('./thirdMiddleware/ntl');
// 启动express
var app = express();
// 设置模版引擎
// 将模版引擎设置为ntl
app.set('view engine', 'ntl');
// 设置模版文件路径
app.set('views', path.join(__dirname, 'public/view'));
// 注册模版引擎ntl
app.engine('ntl', ntl);
// 设置静态资源路径
app.use(express.static(path.join(__dirname, 'public')));
// 设置中间件
app.use(function (req, res, next) {
console.log('[LOG] 过滤石头');
// next('stone is too big');
next();
});
app.use('/water', function (req, res, next) {
console.log('[LOG] 过滤沙子');
next();
});
// post方法,接受post时使用bodyParser
app.use('/post', bodyParser());
// 错误中间件
app.use(function (err, req, res, next) {
console.log('[LOG] err use');
// res.send(err);
next();
});
app.get('/water/:id/:name/home/:age', function (req, res) {
console.log('[LOG] ',req.params);
// res.send(req.query);
res.send(400);
// res.write(JSON.stringify(req.query));
// res.send('water:' + req.hostname + ' ' + req.path);
});
// 模版请求地址
app.get('/template/:id', function (req, res) {
console.log('[LOG] ',req.header('cookie'));
res.render('index', {
title: 'ntl title',
content: 'hello express view engine',
id: req.params.id
});
});
// http get方法 ,路径为/get
app.get('/get', function (req, res) {
res.send('hello');
});
app.post('/post', function (req, res) {
console.log('[LOG] ',req.body);
res.send('this is post method');
});
// 使用重定向
app.get('/login', function (req, res) {
res.redirect('/index')
});
app.get('/index', function (req, res) {
res.send('welcome to index');
});
// 剩余的get方法请求
app.get('*', function (req, res) {
res.send('note find match path');
});
// 监听端口
app.listen(3000);