express初步认识

it2024-04-18  9

1 // 1. 引入 2 const express = require('express'); 3 4 // 2. 创建web服务器 5 const app = express(); 6 7 // 根据路由(路径)处理get和post请求 8 app.get('/', (req, res)=>{ 9 res.writeHead(200, {'Content-Type': 'text/html'}); 10 res.write('<h2>Hello, World!</h2>'); 11 res.end(); 12 }); 13 14 app.get('/like', (req, res)=>{ 15 res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8;'}); 16 res.write('<h2>撩课学院!</h2>'); 17 res.end(); 18 }); 19 20 app.get('/it', (req, res)=>{ 21 res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8;'}); 22 res.write('<h2>like it, it like!</h2>'); 23 res.end(); 24 }); 25 26 27 // 3. 开启监听 28 app.listen(3000, ()=>{ 29 console.log('服务器已经启动!'); 30 });

express中间键

1 // 1. 引入 2 const express = require('express'); 3 const fs = require('fs'); 4 const path = require('path'); 5 6 // 2. 创建web服务器 7 const app = express(); 8 9 // 4. 写日志 10 app.use((req, res, next)=>{ 11 const log = ` 12 ---------------------------------------- 13 1) 请求的方式: ${req.method}, \n 14 2) 请求的路径: ${req.url}, \n 15 3) 请求的时间: ${new Date()}, \n 16 ---------------------------------------- 17 `; 18 // 写入文件 19 fs.appendFile(path.join(__dirname, 'req.log'), log, (err)=>{ 20 if(err) throw err; 21 next(); 22 }); 23 }); 24 25 // 加入很多很多中间件 26 app.use((req, res, next)=>{ 27 console.log('1111111'); 28 next(); 29 }); 30 31 app.use((req, res, next)=>{ 32 console.log('2222222'); 33 next(); 34 }); 35 36 app.use((req, res, next)=>{ 37 console.log('333333'); 38 next(); 39 }); 40 41 42 app.get('/', (req, res, next)=>{ 43 res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'}); 44 res.write('<h2>Hello, World!</h2>'); 45 next(); 46 }); 47 48 app.get('/', (req, res)=>{ 49 res.write('<h2>您好,撩课学院!</h2>'); 50 res.end(); 51 }); 52 53 54 55 // 3. 开启监听 56 app.listen(3000, ()=>{ 57 console.log('服务器已经启动!'); 58 });

服务器搭建网站的初步

1 const express = require('express'); 2 const path = require('path'); 3 const app = express(); 4 5 // 中间件访问静态资源 6 app.use(express.static(path.join(__dirname, 'public'))); 7 8 app.listen(3001, ()=>{ 9 console.log('服务器已经启动'); 10 });

express实现动态的渲染ejs

1 const express = require('express'); 2 const path = require('path'); 3 const app = express(); 4 5 // 注册模板引擎 6 app.set('views', path.join(__dirname, 'views')); 7 app.set('view engine', 'ejs'); 8 9 app.get('/', (req, res)=>{ 10 const dataJson = { 11 "lists":[ 12 {"title": "撩课学院1周年庆倒计时", "count": 675593, "up": 1}, 13 {"title": "女演员全美善自杀", "count": 634434, "up": 1}, 14 {"title": "哈登骑电动车被抓", "count": 623323, "up": 0}, 15 {"title": "吃酸辣粉被罚款", "count": 546767, "up": 0}, 16 {"title": "蔚来汽车庄莉离职", "count": 536237, "up": 1}, 17 {"title": "父母抓阄陪女儿", "count": 525193, "up": 0}, 18 {"title": "宋仲基爸爸短信", "count": 475593, "up": 0}, 19 {"title": "宋仲基爸爸短信", "count": 375593, "up": 1}, 20 {"title": "今天天气很好", "count": 275593, "up": 1} 21 ], 22 "source": "撩课风云榜 - itLike.com" 23 }; 24 res.render('list', dataJson); 25 }); 26 27 app.listen(3000, ()=>{ 28 console.log('服务器已经启动'); 29 });

 

转载于:https://www.cnblogs.com/zhangzhengyang/p/11146242.html

相关资源:数据结构—成绩单生成器
最新回复(0)