Node.js-Tutorial:Node.js

  • g7_485801
    了解作者
  • 15.3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-08 06:23
    上传日期
Node.js:教程 使用本教程作为学习Node.js的指南。 每个单元都包含带注释的课程,其中包含工作示例。 主题 介绍 大事记 溪流 文件系统操作 上载档案 模组 NPM 表示 快速路线 套接字 使用Redis持久化数据 建议的先决条件 介绍 /* Node.js - Allows you to build scalable network applications using JavaScript on the server-side. - Runs on top of the V8 JavaScript Runtime (same that is running on the Chrome browser) What can you build? - Web socket Server - File Upload client - Ad Server - Real
Node_js-Tutorial-master.zip
  • Node.js-Tutorial-master
  • 02 - Events.js
    949B
  • 14 - Persisting Data.js
    784B
  • 10 - Install NPM Modules.js
    1.4KB
  • README.md
    7.3KB
  • 08 - Module Loader.js
    938B
  • 11 - Express.js
    1KB
  • 09 - Http Module.js
    838B
  • 07 - Modules.js
    586B
  • .gitignore
    937B
  • 01 - Introduction.js
    1.6KB
  • 06 - Upload File with Progress.js
    1.4KB
  • 05 - Upload File.js
    745B
  • largeFile.txt
    48.1KB
  • package.json
    224B
  • 13 - Socket.io.js
    1.5KB
  • 03 - Streams.js
    1.5KB
  • socket.html
    707B
  • LICENSE
    1.1KB
  • 04 - File System.js
    857B
  • 12 - Express Routes.js
    1.3KB
内容介绍
<a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/nodejsfull.svg" heigth="300px" width="300px"/>](https://nodejs.org) # Node.js: Tutorial Use this tutorial as a guide to learn Node.js. Each unit contains an annotated lesson with working examples. Topics ================ - Introduction - Events - Streams - File System Manipulation - Uploading Files - Modules - NPM - Express - Express Routes - Socket.io - Persisting Data with Redis Suggested prerequisites ==================== <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/javascript.svg" width="50px" height="50px" />](https://github.com/MartinChavez/Learn-Javascript)</a> Introduction ==================== ```Javascript /* Node.js - Allows you to build scalable network applications using JavaScript on the server-side. - Runs on top of the V8 JavaScript Runtime (same that is running on the Chrome browser) What can you build? - Web socket Server - File Upload client - Ad Server - Real-time data apps Misconceptions - Node.js is not a web framework - Node.js is not multi-threaded The event loop - The first time node interprets the js code and executes it, it registers the events it finds - Once the script has been executed, node starts the event loop, which checks for events continuously - Once node finds a new event, it will trigger the callback associated with such event - Allows us to write code that is non-blocking The event Queue - Queues the events for the event loop - Processes the events, one at a time */ /* How to run node.js */ // In this example, we will create a node server and serve an HTTP response // Use the 'require' keyword to load modules(libraries) var http = require('http'); // In general, you need to specify a call-back function with most of the Node modules methods http.createServer(function (request, response) { response.writeHead(200); //Status code in header response.write("Introduction"); //Response body response.end(); //Close the connection }).listen(8080); //Port in which node will listen for connections console.log('Listening on port 8080...'); /* Run the following cmd to start node and run the server node '01 - Introduction.js' */ /* Run the following cmd to make an HTTP request to your local server (you should get a response) curl http://localhost:8080 */ ``` Events ==================== ```Javascript /* Events - Similar to how the DOM works, Node.js triggers events and handles the callback functions - Many objects in Node emit events, in general, these are inherited from the EventEmitter constructor */ // Loading the EventEmitter constructor var EventEmitter = require('events').EventEmitter; //In this case, we want the logger to emit Events by adding a listener var logger = new EventEmitter(); // The following code demonstrates the syntax for listening to the error event, // and executing the callback function logger.on('error', function(message) { console.log('ERR: ' + message); }); // The following code triggers the 'error' event logger.emit('error', 'This is the first error'); logger.emit('error', 'This is the second error'); /* Run the following cmd to start node and listen/generate events node '02 - Events.js' You should see the following output: ERR: This is the first error ERR: This is the second error */ ``` Streams ==================== ```Javascript /* Streams When writing applications that depend consistently on network access or accessing files on the disk, it is important to understand and optimize how the data is being transferred, this is an excellent use-case for Node.js. Stream: - Streams are like channels, where data flows through - There are two main different types: readable and writeable - Readable stream: Inherits from EventEmitter */ var http = require('http'); // 'request' is a readable stream // 'response' is a writeable stream http.createServer(function(request, response) { // With the following example, we print to the console the data that we get from the client response.writeHead(200); request.on('readable', function(){ var chunk = null; while (null !== (chunk = request.read())) { response.write(chunk); console.log(chunk); } }); request.on('end', function(){ response.end('- end of request'); }); }).listen(8080); //Note: This example could be simplified by using: /* http.createServer(function (request, response) { response.writeHead(200); // pipe helps us write to a writeable stream as soon as you read from a readable stream request.pipe(response); }).listen(8080); */ console.log('Listening on port 8080...'); /* Run the following cmd to start node and run the server: node '03 - Streams.js' Run the following cmd to make an HTTP request to your local server: curl -d 'from client' http://localhost:8080 Expected response: "from client- end of request" */ ``` Modules ==================== ```Javascript /* Modules: - Libraries that are loaded in order to use them in the current context - The 'require' keyword is used to load them */ var moduleFunction = function(){ console.log("Module loaded"); } // In order to expose this method (make it public), we need to use module.exports // 'exports' defines what 'require' returns module.exports = moduleFunction; // Explicitly setting a function as a public method (same behavior as previous example) /* exports.moduleExport = function(){ console.log("Module loaded by using exports"); } */ /* To run, go to "8 - Module Loader" */ ``` Install ==================== ```Terminal npm install ``` Run the tutorial (each file is numbered) ==================== ```Terminal node '01 - Introduction.js' ``` ```Terminal node '02 - Events.js' ``` ```Terminal node '03 - Streams.js' ``` ## Author **[Martin Chavez](https://github.com/MartinChavez)** Continue Learning ==================== <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/javascript.svg" width="50px" height="50px" />](https://github.com/MartinChavez/Learn-Javascript)</a> <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/nodejs.svg" width="50px" height="50px" />](https://github.com/MartinChavez/Node.js-Tutorial)</a> <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/angular.svg" width="50px" height="50px" />](https://github.com/MartinChavez/AngularJs-Basics)</a> <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/angular2.svg" width="50px" height="50px" />](https://github.com/MartinChavez/AngularJS-Advanced-Topics)</a> <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/csharp.svg" width="50px" height="50px" />](https://github.com/MartinChavez/CSharp)</a> <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/linq.svg" width="50px" height="50px" />](https://github.com/MartinChavez/LINQ)</a> <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/jquery.svg" width="50px" height="50px" />](https://github.com/MartinChavez/jQueryBasics)</a> <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/htmlcss.svg" width="65px" height="50px" />](https://github.com/MartinChavez/HTML-CSS)</a> <a name="README" rel='nofollow' onclick='return false;'>[<img src="https://martinchavez.github.io/Assets/Logos/htmlcss2.svg" width="65px" height="50px" />](https://github.com/MartinChavez/HTML-CSS-Advanced-Topics)</a>
评论
    相关推荐
    • Bookmarklet IDE-crx插件
      语言:English (United States) 用于创建/管理书签的IDE 轻松创建和管理您的书签。 在线查找书签?...IDE在您的Chrome书签中使用一个bookmarklet文件夹,因此您仍然拥有完全控制权。 小书签IDE也是主题化的。
    • Declarative Chrome Extension-crx插件
      声明式Chrome扩展程序用于调试基于Siemens Active Workspace Framework的应用程序。 这提供了以下功能。 1.Declarative Inspector检查选定的DOM元素的声明性视图模型属性(动作,ctx,数据,dataProvider,i18n,...
    • chrome 浏览器 selenium IDE插件
      免费下载,官方版本,chrome 浏览器 selenium IDE插件下载包;包含安装说明,简单易懂
    • Shortcuts for Chrome-crx插件
      Chrome快捷方式主要的Chrome页面包括:•历史记录•清除浏览器数据•扩展程序•设置•下载•书签•应用程序(新)•Chrome网上应用店Chrome快捷方式还包括几乎所有其他Chrome页面,这些页面可以在“高级”和“所有”...
    • IDE Support V2.0.9
      JS chrome 调试插件
    • Chrome Textarea Formatter-crx插件
      提供textareas现代IDE中发现的基本代码格式化功能。 如果您曾经尝试编辑在浏览器的textarea字段中截取的格式化代码,则您可能会注意到它的不便之处,因为textareas不支持Tab键格式。 此扩展旨在帮助那些在大多数现代...
    • Bizfone Chrome udvidelse.zip
      语言:dansk ... 它使Bizfone用户更容易进行拨打电话(请记住如果要通过BizFone.dk通知/答案来电,仍然在标签中打开bizfone.dk。 这些数字标有Bizfone标志和来自Bizfone.dk的识别蓝色。... 您还可以标记电话号码并右键单击...
    • macvideo:Chrome合金
      macvideo Chrome合金
    • video_sniffer-chrome
      视频嗅探器 Crappy Chrome插件,可在给定页面上找到所有视频,并为您提供下载按钮。 使用风险自负。 我不负责任,也不在乎您如何处理此事。
    • Continu for Chrome-crx插件
      使用Continu的Chrome扩展程序记录,练习和衡量结果 Continu是专为当今工作场所打造的现代学习管理系统(LMS)。 各种规模的跨国公司都信任Continu,以Continu易于使用,功能强大的学习平台为团队培训提供动力。 通过...