nodejs

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");

http://localhost:3000/

向前端返回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插件使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值