node js基礎筆記

2022-02-23 13:33:27 字數 3769 閱讀 7986

world

console.log('hello world')

//2.定時器

settimeout(()=>,3000)

//3.間隔定時

var time = 0

timer = setinterval(function()

},2000)

var counter = function(arr)

var adder = function(a,b)`

}var pi = 3.14

//1.直接匯出

module.exports = counter

//2.分別匯出

module.exports.counter = counter

module.exports.adder = adder

module.exports.pi = pi

//3.匯出物件

module.exports =

//引用

var test=require('./count')

3.1事件的使用方式

var events = require('events')

var myemitter = new events.eventemitter();

myemitter.on('someevent',function(message))

myemitter.emit('someevent','the event was emitted')

首先匯入events模組

之後使用myemitter進行new出乙個物件

設定someevent這個事件為乙個函式

使用emit進行**函式

3.2.使用場景

var events = require('events')

var util = require('util');

var person = function(name)

util.inherits(person,events.eventemitter)

var xiaoming = new person('xiaoming')

var lili = new person('lili')

var lucy = new person('lucy')

var person = [xiaoming,lili,lucy]

person.foreach(function(person))

})xiaoming.emit('speak','xx')

lucy.emit('speak','sdsda')

lili.emit('speak','hello')

1.檔案讀

var fs = require('fs')

// 同步操作

var readtxt = fs.readfilesync('hellonode/txt','utf8')

fs.writefilesync('hellonode/write',readtxt)

//非同步操作

fs.readfile('hellonode/txt','utf8',(err,data)=>)

2.目錄的建立和刪除

//建立刪除目錄

fs.unlink('hellonode/write',function())

fs.mkdirsync('hello')

fs.rmdirsync('hello')

fs.mkdir('hello',function())

var fs = require('fs')// 輸入輸出流也在fs

var myreadstream = fs.createreadstream('readme.txt')//建立讀取流

var data=''

myreadstream.on('data',function(chunk))

myreadstream.on('end',function())

var http = require('http');//匯入http

var fs = require('fs')//匯入fs

var rs = fs.createreadstream(__dirname + '/index.html')//返回檔案

rs.pipe(res)//使用管道輸出

})server.listen(3000,'127.0.0.1')//在3000埠啟動

console.log('server start on localhost port')

var server = require('./server');

var router = require('./route')

var handler = require('./handle')

var handle = {};//建立物件對映

handle["/"] = handler.home;

handle["/index"] = handler.home;

handle["/review"] = handler.review;

handle["/json"] = handler.json;

server.startserver(router.route,handle);//開啟服務

伺服器server.js

var server = http.createserver(onrequest)//建立乙個server

server.listen(3000,'127.0.0.1')//監聽3000埠

console.log('server start on localhost port')

}exports.startserver = startserver路由route.js

function route(pathname,handle,resp,params) else 

}exports.route = route

handle.js

var fs = require('fs')

function home(res,params));

var rs = fs.createreadstream(__dirname + '/index.html');

rs.pipe(res);

}function review(res,params));

res.write('');

res.end();

}function json(res,params)

module.exports =

nodejs 學習筆記 基礎

本來就沒有寫部落格的習慣,這幾天在學習nodejs,對於一些基本的東西記錄了下來,以備忘記是查閱。1.不要使用 settimeout fn,0 代替 process.nexttick callback 前者比後者效率要低得多 2.util.inherits 實現繼承 var util require...

Nodejs筆記 Nodejs讀取 寫入檔案內容

var fs require fs 流的方式讀取檔案 var readstream fs.createreadstream input.txt var str var count 0 次數 readstream.on data function chunk 讀取完成 readstream.on en...

NodeJS學習筆記

superagent 抓取資源 cheerio 化為dom結構 var superagent require superagent superagent.get host url end function err,sres sres.text 裡面儲存著網頁的 html 內容,將它傳給 cheeri...