什麼是nodejs?

一、什麼是node.js?
Node.js 是一個基於 Chrome V8 引擎的 JavaScript 運行環境。Node.js 使用了一個事件驅動、非阻塞式 I/O 的模型,使其輕量又高效。
Node.js 的包管理器 npm,成爲世界上最大的開放源代碼的生態系統。
Node.js 能夠解析JS代碼(沒有瀏覽器安全級別的限制)提供不少系統級別的API,如:- 文件的讀寫 fs - 進程的管理 procress- 網絡通訊 socket- ……html

二、爲何要學習nodejs?
node-webkit、nodeOs、express、koa、Jade、EJS、Forever、PM二、Log.i、Grunt、Gulp、Webpack、Mocha、Karmanode

三、node.js網
工具:s://nodejs.org/en/
工具安裝:s://www.runoob.com/nodejs/nodejs-install-setup.html
鏈接mongdob:s://www.runoob.com/nodejs/nodejs-mongodb.html
文檔:://nodejs.cn/webpack

四、npm指令
npm -V / npm --version 查詢版本
npm install -g cnpm --registry=https://registry.npm.taobao.org 淘寶鏡像
cnpm -V 查詢版本
npm install supervisor nodemon -g supervisor:用於運行nodejs程序的管理程序 / nodemon:在開發node.js應用程序期間使用的簡單監視器腳本。
npm install pm2 -g 具備內置負載均衡器的Node.JS應用程序的生產過程管理器。 web

五、新建服務器server.js
啓動指令:node server.js/nodemon server.js
let http = require("http"); //node要導入http模塊
let hostname = "localhost"; //主機
let port = 3000; //端口
http.createServer((req, res) =>{mongodb

if(req.url !== "/favicon.ico"){
    console.log(req.url);
    res.writeHead(200, {"Content-Type":"text/html;charset=utf-8"});
    res.write(`<h2>good good study</h2>`)
    res.write(`<h2>day day up</h2>`)
    res.write(`<h2>we are good boy</h2>`)
    res.end();
}

}).listen(port, hostname, () =>{express

console.log(`this server is running at http://${hostname}:${port}`);

})npm

六、什麼叫模塊化開發?
複雜邏輯代碼多的功能分而解之爲獨立高度封裝的單元模塊。瀏覽器

七、CommonJs規範
模塊定義 define
模塊接口暴露 exports
模塊導入 require
注意:
(1)模塊化開發 AMD異步模塊定義(require.js) CMD同步模塊定義(sea.js)
(2) NodeJs && webpack 是 CommonJS 規範 的一種實現 ;CommonJS 規範 是 NodeJs && webpack 的 規範 和規則
(3)全部的 js 文件 自己就是 一個模塊 安全

八、接口暴露
(1)module.exports = {};
let arr = ["小紅", "小黃", "小明"];
let str = "我要飛的更高,飛的更高";
let obj = {服務器

age : "18",
name : "jack",
content : "money"

}
function setUsername(username){

return `${username} 說:every day day up`;

}
module.exports = {

arr,
str,
obj,
setUsername

}
(2)exports key = value;
const getSomeone=(someone)=>{

return  `<h3>${someone} word hard for 12000 RMB </h3>`;

}
exports.getSomeone =getSomeone;
exports.word = <h3>1807 大家努力了嗎</h3>;
(3)export default
export default{

say(){}

}export default function say(){}

相關文章
相關標籤/搜索