1.express
基于nodejs平台、疾速、凋谢、极简的Web开发框架,官网地址(中文版),官网地址
1.根本应用
- 下载express
npm install express --save
引入express模块
let express = require('express');
构建服务实例
// 构建服务实例 相当于 http.createServer();let app = express();
接管服务端申请
// 当服务端收到 get申请 / 的时候,执行回调函数app.get('/',(req,res) => { res.send('Hello World');})
绑定端口
// 绑定端口 相当于http.listen()app.listen(3000,()=> { console.log('server is running...');})
残缺代码
let express = require('express');// 构建服务实例 相当于 http.createServer();let app = express();// 公开指定目录,则能够通过/public间接进行拜访其文件夹内的内容,能够写多个,灵便应用app.use('/public/',express.static('./public/'));// 当服务端收到 get申请 / 的时候,执行回调函数app.get('/',(req,res) => { // 在express中能够间接通过req.query来获取查问字符串参数 console.log(res.query); res.send('Hello World');})// 绑定端口 相当于http.listen()app.listen(3000,()=> { console.log('server is running...');})
如果拜访其余的门路下的内容,
express框架
默认会解决为404
,并显示相干的提示信息。如果要写多个门路下申请的解决,则能够写多个app.get()
,不用像nodejs原生写http服务一样本人判断。同时如果要对某个门路下的资源进行凋谢,能够采取以下的代码进行配置// 公开指定目录,则能够通过/public间接进行拜访其文件夹内的内容,能够写多个,灵便应用// 第一个参数配置客户端能怎么样进行拜访,第二个参数是服务器端绝对于以后文件的文件门路app.use('/public/',express.static('./public/'));