1、下载
Node.js — Run JavaScript Everywhere
2、环境搭建
node -v 查看版本
npx 运行外连接
3、小试
运行:node test.js
4、全局变量
console.log(__dirname);//打印当前所在目录
console.log(__filename);//打印全路径
5、外部引用
node js中引入外部,不能使用SE6中的import,会报
Cannot use import statement outside a module
需要被引用的文件
fnTool.js
let sum_fn = function(a,b){
return a+b;
}
module.exports = sum_fn
test.js
console.log("=======node==开始======");
let sum_fn = require("./fnTool.js");
console.log(sum_fn(3,5));
6、文件的读写
同步读写
var readMe = fs.readFileSync("./fnTool.js","utf-8");
console.log(readMe)
异步读写
//====================异步=============
var reader = fs.readFile("./fnTool.js","utf-8",function(err,data){
fs.writeFile("./newFile1.js",data,function(){
console.log("读写完成");
});
});
7、文件流 和 管道
文件流:
读:
let readerStream = fs.createReadStream(__dirname+"/fnTool.js","utf-8");
let data = "";
readerStream.on("data",function(chunk){
data+=chunk;
});
readerStream.on("end",function(){
console.log(data);
});
写:
var writerStream = fs.createWriteStream(__dirname+"/newFile.js","utf-8");
writerStream.write("NODE JS");
writerStream.end();
writerStream.on("finish",function(){
console.log("写入完成");
});
读写:
let readerStream = fs.createReadStream(__dirname+"/fnTool.js","utf-8");
var writerStream = fs.createWriteStream(__dirname+"/newFile.js","utf-8");
let data = "";
readerStream.on("data",function(chunk){
writerStream.write(chunk);
});
readerStream.on("end",function(){
console.log("读出完成");
});
管道写:
let readerStream = fs.createReadStream(__dirname+"/fnTool.js","utf-8");
var writerStream = fs.createWriteStream(__dirname+"/newFile.js","utf-8");
readerStream.pipe(writerStream);
8、Web服务请求
console.log("=======node==开始======");
let http = require("http");
var server = http.createServer(function(req,res){
console.log("la la la");
});
server.listen(3000,"127.0.0.1");
访问它
后端就会显示:
向前端返回字符:
console.log("=======node==开始======");
let http = require("http");
var server = http.createServer(function(req,res){
//设置返回内容
res.writeHead(200,{
"Content-Type":"text/plain"
});
res.end("Hello world");
});
server.listen(3000,"127.0.0.1");
向前端返回HTML:
console.log("=======node==开始======");
let http = require("http");
let fs = require("fs");
var server = http.createServer(function(req,res){
//设置返回内容
res.writeHead(200,{
"Content-Type":"text/html"
});
var reader = fs.createReadStream(__dirname+"/list.html");
reader.pipe(res);//直接通过管道写入 返回
});
server.listen(3000,"127.0.0.1");
向前端返回json:
let http = require("http");
let fs = require("fs");
var server = http.createServer(function(req,res){
//设置返回内容
res.writeHead(200,{
"Content-Type":"text/json"
});
let json = {a:"1",b:"2",c:"中国"};
res.end(JSON.stringify(json));
});
server.listen(3000,"127.0.0.1");
9、事件驱动
const Events = require("events");
const myEvent= new Events();
myEvent.on("done",()=>{
console.log("事件执行完成");
});
myEvent.on("done",()=>{
console.log("事件执行完成22");
});
myEvent.emit("done");
10、buffer
console.log("=======node==开始======");
let buf = Buffer.alloc(6);
buf.fill('3')
console.log(buf.toString())
let b1 = Buffer.from('中国');
let b2 = Buffer.from('人民');
let b = Buffer.concat([b1,b2],9);
console.log(b.toString());
nodemon插件使用