Express框架

express的三个功能:

  • static静态文件访问,路径拦截
  • 全栈模板 jade ejs
  • 对get  post  支持

$npm install --save--dev express

第一功能:网址访问接收

//配置文件的访问

//导入包
var express = require("express");
//实例化express对象
var app = express();

app.get("/",function(req,res){

	res.send("成功访问");

}).listen(3000);

第二:static文件夹文件直接访问

假如static文件夹下有index.html,它会默认匹配“/”路径

//设置静态文件夹,直接网址敲
app.use(express.static("static"));
//配置文件的访问

//导入包
var express = require("express");
//实例化express对象
var app = express();

//设置静态文件夹,直接网址敲
app.use(express.static("static"));

app.get("/",function(req,res){

	res.send("成功访问");

}).listen(3000);

app.post("/",function(req,res){

	res.send("成功访问");

}).listen(3000);

第三:拦截器过滤器

//拦截器:过滤器
app.use("/",function(req,res,next){
	console.log("拦截器1");
	//事务继续往下执行
	next();
})

app.use("/test",function(req,res,next){
	console.log("拦截/test的拦截器");
	//事务继续往下执行
	next();
})
//配置文件的访问

//导入包
var express = require("express");
//实例化express对象
var app = express();

//设置静态文件夹,直接网址敲
app.use(express.static("static"));

//拦截器:过滤器
app.use("/",function(req,res,next){
	console.log("拦截器1");
	//事务继续往下执行
	next();
})

app.use("/test",function(req,res,next){
	console.log("拦截/test的拦截器");
	//事务继续往下执行
	next();
})

app.get("/",function(req,res){

	res.send("成功访问");

}).listen(3000);

第四建模:

jade难点     npm install --save--dev jade

//建模
//全站建模:jade,ejs两种
//npm install --save--dev jade
app.set("view engine","jade");
//设置模板引擎类型
//创建一个jade文件夹,并设置该文件夹的路径
app.set("views",__dirname+"/jade");
doctype html
html(lang="en")
	head
		title #{titlex}
		meat(name="keywords",content="kgc 课工场")
		script(type="text/javaScript").
			var a=100
			console.log(a);

			function clickBtn(){
			alert("我被点击了");
			}
		style.
			h1{
			color:red;
			}
		link(type="text/css",href="")

	body
		h1 课工场
		div.col#test
			a(href="http://www.baidu.com" target="_bank" style="{color:red}") 百度
			br
			|这是文本需要在前面加个竖杠
			br
			button(onclick="clickBtn()") 点击
			-for(var i=0;i<3;i++)
				div 第#{i}次循环,嵌入脚本需要在前面加短横杠

			-var str = "课工场"
				div=str

			-var a=false,b=false
				if a
					div a is true
				else if b
					div b is true
				else
					div all are false

			-var arr=["a","b","c"]
				each item in arr
					div=item

			

这是jade的书写格式:

  1. 父子标签关系是用tab缩进来确定的
  2. div.col#test  相当于 class=col  id=test
  3. 前面加 | 表示后面的内容都是文本内容
  4. 标签空一下格,在写就是该标签的文本内容

ejs 模板

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>EJS</title>
</head>
<body>
	<%=titlex%>
</body>
</html>

ejs模板是跟html格式书写是一样的,就像jsp嵌入java代码一样,直接用<%%>括起来,在内部就能写脚本代码

app.get("/",function(req,res){
	console.log(__dirname);
	res.render("index",{titlex:"jade测试成功"})

第四:处理post get

get

http://localhost:3000/?jin=123

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>首页</title>
	<script src="https://cdn.bootcss.com/jquery/1.8.0/jquery.min.js"></script>
</head>
<body>
	<button onclick="dian()">点击</button>
	<script>
		function dian(){
			$.ajax({
				url:"http://localhost:3000/index",
				type:"get",
				data:{"name":"jinq","pass":"123"},
				success:function(data){
					alert(data);
				}
			})
		}
	</script>
</body>

 

var express = require("express");
var app = express();

app.get("",function(req,res){
	console.log(req.query.jin);


}).listen(3000)

post

$ npm install --save-dev body-parser

//安装解析post的插件
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended:false}));
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>首页</title>
	<script src="https://cdn.bootcss.com/jquery/1.8.0/jquery.min.js"></script>
</head>
<body>
	<button onclick="dian()">点击</button>
	<script>
		function dian(){
			$.ajax({
				url:"http://localhost:3000/index",
				type:"post",
				data:{"name":"jinq","pass":"123"},
				success:function(data){
					alert(data);
				}
			})
		}
	</script>
</body>

var express = require("express");
var app = express();
//设置静态文件夹,直接网址敲
app.use(express.static("static"));

//安装解析post的插件
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended:false}));



app.post("/index",function(req,res){
	console.log(req.body);
	console.log(req.body.name)
	res.send("success")
}).listen(3000);

使用上传文件插件的步骤:

html端

<form action="http://localhost:3000/upload" method="post" enctype="multipart/form-data">
		<input type="file" name="image" size="50" />
		<br />
		<input type="submit" value="上传文件" />
	</form>

node.js端

var express = require("express");
var app = express();
var multer  = require('multer');

var bodyParser = require("body-parser");
//安装解析post的插件
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended:false}));

app.use(multer({ dest: '/tmp/'}).array('image'));



app.use(express.static("static"));

app.get("/",function(req,res){
	res.end("GET")
}).listen(3000);

app.post("/upload",function(req,res){
	
	// console.log(req.file)
	console.log(req.files);  // 上传的文件信息
	res.end("POST")
})

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值