Node:
Express -> webStorm ->提供了这个模板
npm:node的包管理工具 ->可以下载(发布)基于node的插件
可以使用前端的技术写服务端
模块化开发
模块化开发的标准:
AMD
CMD
Common.js
Node搭建本地服务器
服务器怎么去访问?通过主机地址加上端口号去访问localhost:3333,
在访问本地服务器的时候需要开启服务器->开启服务器的方法:node文件名
每一次在修改服务器里面内容的时候都需要重启服务器->停止服务器:control+c
require('http').createServer(function(req,res){
res.end('这是我的第一个node服务端工程')
}).listen(3333)
使用express-generator创建node的工程
Bin/www是服务器的启动文件
Routes:写服务端API的地方
Localhost:3000/user
Localhost:3000/user/register
Localhost:3000/user/loginLocalhost:3000/index
Localhost:3000/message
App.js配置路由的地方
第一个客户端与服务端通讯的API
服务端:
/**
*request:客户端发送给服务端的内容
*response:服务端发送给客户端的内容
*/
router.get('/register',function(request,response){
console.log(request);
response.send({
code:200,
message:'注册成功',
data:{
id=100
}
});
})
客户端:
var HOST='http://localhost:3000/';
var REGISTER='users.register';
document.querySelector('.registerButton').onclick=function(){
var request=new XMLHttpRequest();
request.open('GET',HOST+REGISTER);
request.onload=function(){
console.log(this.response);
}
request.send();
}
注意:只要是不同端口或者不同主机地址互相访问数据会出现访问跨域
Corse(设置允许请求的主机地址origin)jsonP(json类型的数据交换格式)