温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nodejs渐入佳境[15]-express框架

发布时间:2020-07-20 08:52:19 来源:网络 阅读:214 作者:jonson_jackson 栏目:开发技术

最简单的服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const express = require('express');

var app = express();


//返回html格式
app.get('/',(req,res)=>{
 res.send('<h2>Hello world</h2>');
});

//返回json格式
app.get('/fast',(req,res)=>{
 res.send({
     name:'json',
     likes:[
       'reading',
       'coding'
     ]
 });
});
//监听端口
app.listen(3000);

访问:
localhost:3000
localhost:3000/fast

访问静态文件

创建public/help.html

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en" dir="ltr">
 <head>
   <meta charset="utf-8">
   <title></title>
 </head>
 <body>
   <h2>Hello Jonson</h2>
 </body>
</html>

express.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
const express = require('express');

var app = express();

// 参数是一个middleware
app.use(express.static(__dirname +'/public'));
//返回html格式
app.get('/',(req,res)=>{
 res.send('<h2>Hello world</h2>');
});

//返回json格式
app.get('/fast',(req,res)=>{
 res.send({
     name:'json',
     likes:[
       'reading',
       'coding'
     ]
 });
});
//监听端口,  第二个回调是开启服务器后调用
app.listen(3000,()=>{
 console.log('hello jonson');
});

访问:
http://localhost:3000/help.html
会打开public/help.html的页面并显示出来。

动态注入 express template engines

1
npm install --save hbs

新建views/about.hbs:

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html dir="ltr">
 <head>
   <meta charset="utf-8">
   <title></title>
 </head>
 <body>
   <h2>{{currentYear}}</h2>
   <footer>{{pageTitle}}</footer>
 </body>
</html>

express.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
const express = require('express');
const hbs = require('hbs');
var app = express();

app.set('view engine','hbs');




// 参数是一个middleware
app.use(express.static(__dirname +'/public'));
//返回html格式
app.get('/',(req,res)=>{
 res.send('<h2>Hello world</h2>');
});

//返回json格式
app.get('/fast',(req,res)=>{
 res.send({
     name:'json',
     likes:[
       'reading',
       'coding'
     ]
 });
});

//动态传递参数。
app.get('/about',(req,res)=>{
 res.render('about.hbs',{
   pageTitle:'About Page',
   currentYear:new Date().getFullYear()
 });
});
//监听端口,  第二个回调是开启服务器后调用
app.listen(3000,()=>{
 console.log('hello jonson');
});

访问:
localhost/about

模版封装

新建:views/partial/footer.hbs:

1
2
3
<Header>
   <footer>{{pageTitle}}</footer>
<Header>

view/abut.hbs:

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en" dir="ltr">
 <head>
   <meta charset="utf-8">
   <title></title>
 </head>
 <body>
   <h2>{{currentYear}}</h2>
     {{> footer}}
 </body>
</html>

express.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const express = require('express');
const hbs = require('hbs');
var app = express();

hbs.registerPartials(__dirname + '/views/partials');
app.set('view engine','hbs');
// 参数是一个middleware
app.use(express.static(__dirname +'/public'));


//返回json格式
app.get('/about',(req,res)=>{
 res.render('about.hbs',{
   pageTitle:'About Page',
   currentYear:new Date().getFullYear()
 });
});
//监听端口,  第二个回调是开启服务器后调用
app.listen(3000,()=>{
 console.log('hello jonson');
});

访问:
localhost/about

express middleware

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
const express = require('express');
const hbs = require('hbs');
const fs = require('fs');
var app = express();


hbs.registerPartials(__dirname + '/views/partials');
app.set('view engine','hbs');
// 参数是一个middleware
app.use(express.static(__dirname +'/public'));
//返回html格式
app.get('/',(req,res)=>{
 res.send('<h2>Hello world</h2>');
});

//自定义middleware
app.use((req,res,next)=>{
 var now = new Date().toString();
 var log = `${now}:${req.method} ${req.url}`;
 console.log(log);
 fs.appendFile('server.log',log+'\n',(err)=>{});
 next();

});
//返回json格式
app.get('/fast',(req,res)=>{
 res.send({
     name:'json',
     likes:[
       'reading',
       'coding'
     ]
 });
});

//返回文件,about.hbs在views文件夹下
app.get('/about',(req,res)=>{
 res.render('about.hbs',{
   pageTitle:'About Page',
   currentYear:new Date().getFullYear()
 });
});
//监听端口,  第二个回调是开启服务器后调用
app.listen(3000,()=>{
 console.log('hello jonson');
});
  • 本文链接: https://dreamerjonson.com/2018/11/15/node-15-express/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

nodejs渐入佳境[15]-express框架

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI