1、文件系统读取文件(fs)
使用fs需要在js文件中引入fs
var fs = require('fs');
//nodejs读取文件的操作是异步的,所以会先执行console.log("hello world")
fs.readFile('./demo.txt', (error, data) => {
if(error){
console.log(error);
}else{
console.log(data);
}
})
console.log("先执行这条代码");
2、事件处理机制
使用events需要在js文件中引入events
var events = require("events"); //引入events
var eventEmitter = new events.EventEmitter();
eventEmitter.on("myEvent", (data)=>{ //定义"myEvent"事件
console.log("execute myEvent");
console.log(data);
})
eventEmitter.emit("myEvent", "zhangsan"); //触发"myEvent"事件
根据传的参数判定执行哪个代码
var events = require("events");
var myevent = new events.EventEmitter();
myevent.on("getdata", function(methodname){
if(methodname == 'local'){
console.log("local");
}else if(methodname == 'online'){
console.log("online");
}
})
myevent.emit("getdata", "local")
myevent.emit("getdata", "online")
3、Nodejs创建服务器
var http = require("http");
http.createServer((req,res) => {
res.write("hello world");
res.end();
}).listen(8001)
3.1、get请求处理过程
get请求需要拿到传过来的参数,这个时候需要使用querystring或者url插件,使用npm install直接下载使用
var http = require("http");
var url = require("url");
// var querystring = require("querystring");
http.createServer((req,res)=>{
res.writeHead(200,{"Content-Type": "text/html;charset=utf8"})
var reqparams = url.parse(req.url,true);
// var queryparams = querystring.parse(reqparams.query);
// console.log(reqparams.query.username);
var username = reqparams.query.username;
var passward = reqparams.query.pwd;
res.end("用户名:"+username + "密码:"+passward);
}).listen(8001)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>表单提交</title>
</head>
<body>
<form method="get" action="http://localhost:8001/">
<input type="text" name="username">
<input type="password" name="pwd">
<input type="submit" value="提交">
</form>
</body>
</html>
3.2、post 请求处理过程
使用post提交请求同样需要使用到querystring,下载使用npm install
var http = require("http");
var querystring = require("querystring");
http.createServer((req,res) => {
var post = "";
// post请求接收数据
req.on("data", function(chunk){ //因为在数据传输的时候,是以数据流的形式传输
post += chunk;
})
req.on("end", function(){
post = querystring.parse(post);
console.log(post);
res.writeHead(200,{"Content-Type": "text/html;charset=utf8"})
res.end("用户名:"+ post.username + " 密码:"+ post.pwd);
})
}).listen(8002)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>post表单</title>
</head>
<body>
<form method="post" action="http://localhost:8002/">
<input type="text" name="username">
<input type="password" name="pwd">
<input type="submit" value="提交">
</form>
</body>
</html>
4、数据库mysql直连以及实现登录模块
<div class="main">
<div class="login_wrapper">
<div class="login_left">
<form method="post" action="http://localhost:8001/">
<div class="login_name">
用户名:<input type="text" name="name">
</div>
<div class="login_name">
密码:<input type="passward" name="pwd">
</div>
<input type="submit" value="提交">
</form>
</div>
</div>
</div>
var http = require("http");
var querystring = require("querystring");
var mysql = require("mysql");
http.createServer((req,res)=>{
var post = "";
req.on("data", (chunk)=>{
post += chunk;
})
req.on("end", ()=>{
var formObject = querystring.parse(post);
var username = formObject.name;
var pwd = formObject.pwd;
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "5qbtsGKz%qa9",
database: "mysystem"
})
connection.query("select * from loginusers where username=? and password=?",[username,pwd], (err, data)=>{
if(err){
console.log(err);
}else if(data.length>0){
res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
res.write("登录成功");
res.end();
}else{
res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
res.write("暂无数据");
res.end();
}
})
})
}).listen(8001);