标签:回调 modules func files serve 一个 自动 javascrip 非阻塞
https://nodejs.org/zh-cn/
https://nodejs.org/dist/
console.log("Hello World");
require('http')
http.createServer()
server.listen(8888)
node server.js
var http = require('http');
var server = http.createServer((request, response)=>{
response.writeHead(200, {"Content-Type": "text/plain"});
response.end('Hello World');
});
server.listen(8888);
console.log('服务启动。。。 127.0.0.1:8888');
① 包下载
npm install 包名
npm install 包名 -g # 全局安装
② 下载源
npm config get registry # 获取下载源地址
npm config set registry https://registry.npm.taobao.org/ # 修改淘宝下载源
③ 安装位置的查看
npm root -g
安装地址修改
[node安装位置]\node_modules\npm中的 npmrc 文件修改为: prefix=D:\Program Files\npm
var fs = require('fs');
fs.readFileSync('sync.txt');
console.log(data.toString());
console.log('结束');
var fs = require('fs');
fs.readFile('sync.txt', (err, data) => {
if (err) throw err;
console.log(data.toString());
});
console.log('结束');
实现事件的监听及触发功能
event.on('eventName', function(){});
event.emit('eventName');
var events = require('events');
var eventEmitter = new events.EventEmitter();
eventEmitter.on("eventName", ()=>{
console.log("触发了");
});
console.log("将要触发");
eventEmitter.emit("eventName");
console.log("my module run");
exports.word = 'Hello Module';
模块的引用顺序
require('./my_module')
var my_module = require('./my_module');
var my_module = require('./my_module');
var fs = require('fs');
// 从当前文件开始到系统根目录依次查找 node_modules这个文件夹是否有我们想要的数据
var http = require('http');
var server = http.createServer((request, response)=>{
response.end('Index')
});
server.listen(3000);
request.url
var url = request('url');
var param = url.parse(request.url, true);
console.log(param.query);
var http = require('http');
var url = require('url');
var server = http.createServer((request, response) => {
if (request.url === '/node') {
response.end('node');
} else if (request.url === '/') {
response.end('Index');
}
var param = url.parse(request.url, true);
console.log(param.query.md);
console.log(param.query.pw);
});
server.listen(3000);
标签:回调 modules func files serve 一个 自动 javascrip 非阻塞
原文地址:https://www.cnblogs.com/GetcharZp/p/11735567.html