溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2020-07-20 08:52:19 來源:網(wǎng)絡 閱讀:222 作者:jonson_jackson 欄目:開發(fā)技術

最簡單的服務器

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'
     ]
 });
});
//監(jiān)聽端口
app.listen(3000);

訪問:
localhost:3000
localhost:3000/fast

訪問靜態(tài)文件

創(chuàng)建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();

// 參數(shù)是一個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'
     ]
 });
});
//監(jiān)聽端口,  第二個回調是開啟服務器后調用
app.listen(3000,()=>{
 console.log('hello jonson');
});

訪問:
http://localhost:3000/help.html
會打開public/help.html的頁面并顯示出來。

動態(tài)注入 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');




// 參數(shù)是一個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'
     ]
 });
});

//動態(tài)傳遞參數(shù)。
app.get('/about',(req,res)=>{
 res.render('about.hbs',{
   pageTitle:'About Page',
   currentYear:new Date().getFullYear()
 });
});
//監(jiān)聽端口,  第二個回調是開啟服務器后調用
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');
// 參數(shù)是一個middleware
app.use(express.static(__dirname +'/public'));


//返回json格式
app.get('/about',(req,res)=>{
 res.render('about.hbs',{
   pageTitle:'About Page',
   currentYear:new Date().getFullYear()
 });
});
//監(jiān)聽端口,  第二個回調是開啟服務器后調用
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');
// 參數(shù)是一個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()
 });
});
//監(jiān)聽端口,  第二個回調是開啟服務器后調用
app.listen(3000,()=>{
 console.log('hello jonson');
});
  • 本文鏈接: https://dreamerjonson.com/2018/11/15/node-15-express/

  • 版權聲明: 本博客所有文章除特別聲明外,均采用 CC BY 4.0 CN協(xié)議 許可協(xié)議。轉載請注明出處!

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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI