安装:位于node_modules 目录
D:\NodeJs>cnpm install express --save
√ Installed 1 packages
√ Linked 46 latest versions
√ Run 0 scripts
Recently updated (since 2018-02-15): 4 packages (detail see file D:\NodeJs\node_modules\.recently_updates.txt)
√ All packages installed (50 packages installed from npm registry, used 4s, speed 41.7kB/s, json 47(103.8kB), tarball 70.47kB)
D:\NodeJs>
http的get/post请求:
//引入express web框架
var express = require('express');
// 引入 events 模块
var events = require('events');
var bodyParser = require('body-parser');
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var application = express();
application.get('/',function(request,response){
response.send('test~~~~');
});
application.get('/list',function(request,response){
console.log("请求list.html的GET页面");
response.send('这是list.html页面~~~~');
});
application.get('/login.html',function(request,response){
console.log("请求login.html的GET页面");
response.sendFile(__dirname + "/" + "./login.html");
});
application.post('/login_post',urlencodedParser,function(request,response){
console.log("请求login.html的POST页面");
//JSON 格式
var data ={"username":request.body.username,"password":request.body.password};
console.log(data);
response.writeHead(200,{'Content-Type':'text/plain;charset=utf-8'});
response.end(JSON.stringify(data));
response.send('这是login.html页面~~~~');
});
var server = application.listen(8888,function(){
var host = server.address().address;
var port = server.address().port;
console.log("访问地址为 http://%s:%s", host, port);
});
login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login页面</title>
</head>
<body>
<form action="http://127.0.0.1:8888/login_post" method="POST">
username: <input type="text" name="username"> <br/>
password: <input type="password" name="password"> <br/>
<input type="submit" value="Submit">
</form>
</body>
</html>