nodejs(6)express学习
1.简单认识express
express::一个快速的网站开发框架,封装了原生的http模块,用起来更方便;API更人性化
特点
基于Node.js平台之上,进一步封装了
http模块,从而提供了更好用,更友好的 API使用Express创建网站,比使用原生的http模块更加方便;
Express 并没有覆盖 原生 http 模块中的方法,而是基于 原生方法之上,做了更友好的封装,让用户体验更好
创建服务器

// npm install express -S
const express = require('express')
// 创建服务器
const app = express()
// 监听客户端的请求
// 只有客户端的请求类型是 get,并且 请求地址是 / 根路径的时候,
// 才会调用 后面指定的处理函数
app.get('/', (req, res) => {
// express 中,封装了更好用的 res.send 方法
res.send('你好,express 服务器!')
})
// 监听客户端的post请求,并且请求的地址是 /adduser 的时候,
// 才会调用后面指定的处理函数
app.post('/adduser', (req, res) => {
res.send('服务器处理成功!')
})
// 启动服务器
app.listen(4444, () => {
console.log('express server running at http://127.0.0.1:4444')
})
express 中的快捷方法
res.send()
支持 发送 字符串
Content-Type: text/html;支持 发送 对象 或 数组
Content-Type: application/json支持 发送 Buffer 此时会当作文件下载;
const fs = require('fs')
const path = require('path')
// 导入 express 模块
const express = require('express')
// 创建 express 的服务器实例
const app = express()
app.get('/', (req, res) => {
// 1.发送普通文本
// res.send('你好')
// 2.发送对象或数组
// res.send({ name: 'zs', age: 30 })
// res.send(['zs', 'ls', 'zl'])
// 3.发送Buffer二进制 1. 得到二进制 2. 使用send发送二进制
fs.readFile(path.join(__dirname, './小明.mp3'), (err, buf) => {
if (err) return res.send('发送文件失败!')
// 发送 Buffer 二进制
res.send(buf)
})
})
// 调用 app.listen 方法,指定端口号并启动web服务器
app.listen(3001, function() {
console.log('Express server running at http://127.0.0.1:3001')
})
res.sendFile()
用法1:
res.sendFile(path.join(__dirname, './view/index.html'))用法2:
res.sendFile('./view/movie.html', { root: __dirname })注意:
res.sendFile()可以向浏览器发送 静态页面;
使用res.sendFile发送文件
// 导入 express 模块
const express = require('express')
const path = require('path')
// 创建 express 的服务器实例
const app = express()
app.get('/', (req, res) => {
// 向客户端发送文件
// sendFile 必须发送绝对路径,或提供 root 选项
// res.sendFile(path.join(__dirname, './views/movie.html'))
// 或者
// res.sendFile('./views/movie.html', { root: __dirname })
// 实现下载功能,使用sendFIle
// res.sendFile(path.join(__dirname, './New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3'))
const filename = encodeURI('歌曲.mp3')
res.sendFile('./New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3', { root: __dirname, headers: { 'Content-Disposition': 'attachment; filename=' + filename } })
// res.download('./New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3', 'aaa.mp3', err => {
// if (err) return console.log('文件下载失败')
// console.log('文件下载成功')
// })
})
// 调用 app.listen 方法,指定端口号并启动web服务器
app.listen(3001, function() {
console.log('Express server running at http://127.0.0.1:3001')
})
向客户端发送文件
const express = require('express')
const path = require('path')
const app = express()
app.get('/', (req, res) => {
// res.sendFile('直接传递一个绝对路径')
// res.sendFile(path.join(__dirname, './views/movie.html'))
/* const name = encodeURI('一首歌曲.flac')
res.sendFile('./苏醒 - Stand Up Again.flac', {
root: __dirname,
headers: {
'Content-Disposition': 'attachment; filename=' + name
}
}) */
res.download('./New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3', 'Battle.mp3', err => {
if (err) return console.log('文件下载失败!')
console.log('下载完成!')
})
})
app.listen(3001, () => {
console.log('server running at http://127.0.0.1:3001')
})
点击下载歌曲
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.sendFile('./music.html', { root: __dirname })
})
// 监听客户端的下载请求,返回一个具体的文件
app.get('/dowload/music', (req, res) => {
res.download('./New Divide - J2 Feat_ Avery _ Alita- Battle Angel.mp3', 'Battle Angel.mp3', err => {
if (err) return console.log('失败了!')
console.log('ok')
})
})
app.listen(3001, () => {
console.log('server running at http://127.0.0.1:3001')
})
music.html
<a href="/dowload/music">下载音乐</a>
使用express.static快速托管静态资源
const express = require('express')
const app = express()
//#region 注释
/* app.get('/', (req, res) => {
res.sendFile('./views/home.html', { root: __dirname })
})
app.get('/movie.html', (req, res) => {
res.sendFile('./views/movie.html', { root: __dirname })
})
app.get('/about.html', (req, res) => {
res.sendFile('./views/about.html', { root: __dirname })
}) */
//#endregion
// 使用 app.use 来进行相关的配置
// app.use(express.static('./views'))
// 步骤的拆解
const result = express.static('./views')
app.use(result)
// 再次托管一下样式表的资源目录
app.use('/css', express.static('./css'))
// 托管JS文件目录
app.use('/js', express.static('./js'))
// vue 打好的包,直接放在一个文件夹下
// 然后app.use(express.static('./views'))使用就可以了
app.listen(3001, () => {
console.log('server running at http://127.0.0.1:3001')
})
nodejs(6)express学习的更多相关文章
- [转] NodeJS框架express的途径映射(路由)功能及控制
NodeJS框架express的路径映射(路由)功能及控制 我们知道Express是一个基于NodeJS的非常优秀的服务端开发框架,本篇CSSer将提供express框架的route和route co ...
- NodeJS 框架 Express 从 3.0升级至4.0的新特性
NodeJS 框架 Express 从 3.0升级至4.0的新特性 [原文地址:√https://scotch.io/bar-talk/expressjs-4-0-new-features-and-u ...
- npm install Error:EPROTO: protocol error, symlink '../mime/cli.js' -> '/vagrant/src/nodejs/node_modules/express/node_modules/send/node_modules/.bin/mime'
我在ubuntu上使用npm安装依赖是出现下面错误: npm ERR! Linux 3.13.0-101-genericnpm ERR! argv "/usr/bin/nodejs" ...
- express学习(三)—— cookie和session
express学习(三)-- cookie和session cookie存在浏览器中,最大只能保存4K数据,不安全 session存在服务器中,不能独立(先读取cookie再读取session),较安 ...
- 阿里云主机Nginx下配置NodeJS、Express和Forever
https://cnodejs.org/topic/5059ce39fd37ea6b2f07e1a3 AngularJS中文社区即运行在阿里云主机上,本站使用Nginx引擎,为了AngularJS,我 ...
- nodejs的Express框架源码分析、工作流程分析
nodejs的Express框架源码分析.工作流程分析 1.Express的编写流程 2.Express关键api的使用及其作用分析 app.use(middleware); connect pack ...
- 知名nodeJS框架Express作者宣布弃nodeJS投Go
知名 nodeJS 框架 Express 的作者 TJ Holowaychuk 在 Twitter 发推并链接了自己的一篇文章,宣布弃 nodeJS 投 Go. 他给出的理由是:Go 语言和 Rust ...
- nodejs下express+ejs环境搭建
nodejs下express+ejs环境搭建 分类: Nodejs 1.进入需要创建项目的目录 cd F:\nodeCode 2.创建一个带ejs模板工程,工程名为haha e ...
- NodeJS 第一天学习
NodeJS 第一天学习 严格模式 ECMAScript 5的严格模式是采用具有限制性JavaScript变体的一种方式,从而使代码显示地 脱离"马虎模式/稀松模式/懒散模式"(s ...
- nodejs库express是如何接收inbound json请求的
这样几行简单的代码创建一个web服务器: var express = require('express'); var app = express(); var server = require('ht ...
随机推荐
- imp.load_source的用法
imp.load_source(name,pathname[,file])的作用把源文件pathname导入到name模块中,name可以是自定义的名字或者内置的模块名称. 假设在路径E:/Code/ ...
- 吴裕雄--天生自然java开发常用类库学习笔记:国际化程序
import java.util.ResourceBundle ; public class InterDemo01{ public static void main(String args[]){ ...
- RadioButton之互斥选择和Toast显示
前言: RadioButton用来单选并且用Toast来进行提示所选内容 RadioButton标签单独写的时候不能出现互斥现象,代码如下 <RadioButton android:layout ...
- P 1007 素数对猜想
转跳点:
- HihoCoder第十周:后序遍历
也就在大二学数据结构的时候知道了树的前序遍历.后序遍历.中序遍历.之后就忘了,在之后就是大四研究生老师考我,我当时还不知道,真够丢人的.自此之后,知道了如何通过其中两个得到第三个,但是也没有编程实现过 ...
- node - 读取cookie
req.headers.cookie
- Oracle 序列(查询序列的值,修改序列的值)
1.序列的语法形式 create sequence 序列名 increment by n start with n maxvalue n | nomaxvalue minvalue n | nomin ...
- springboot#filter
_ Filter不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对HttpServletResponse 进行后处理,是个典型的处理链.完 ...
- Spring Boot -- Spring Boot之@Async异步调用、Mybatis、事务管理等
这一节将在上一节的基础上,继续深入学习Spring Boot相关知识,其中主要包括@Async异步调用,@Value自定义参数.Mybatis.事务管理等. 本节所使用的代码是在上一节项目代码中,继续 ...
- 四十三、在SAP中初始化勾选值
一.上代码 二.运行时,勾选框会被自动勾选中 三.表单如下