Home  >  Article  >  Web Front-end  >  What is the difference between nodejs and websocket

What is the difference between nodejs and websocket

青灯夜游
青灯夜游Original
2021-12-31 11:28:112535browse

Difference: 1. nodejs is a JS running environment, while WebSocket is a communication protocol; 2. nodejs is used to easily build network applications with fast response and easy expansion, while WebSocket can make the client Data exchange with the server becomes simpler, allowing the server to actively push data to the client.

What is the difference between nodejs and websocket

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

What is nodejs

Node is a JavaScript running environment based on the Chrome V8 engine, a development platform that allows JavaScript to run on the server. Used to easily build network applications with fast response and easy expansion. Node uses an event-driven, non-blocking I/O model to be lightweight and efficient, making it ideal for running data-intensive real-time applications on distributed devices.

Node uses an architecture called an "event loop", making it easy and safe to write highly scalable servers. There are a variety of techniques for improving server performance. Node has chosen an architecture that can both improve performance and reduce development complexity. This is a very important feature. Concurrent programming is often complex and full of landmines. Node bypasses these but still provides great performance.

Node uses a series of "non-blocking" libraries to support the event loop. Essentially, it provides interfaces for resources such as file systems and databases. When sending a request to the file system, there is no need to wait for the hard disk (to address and retrieve the file). The non-blocking interface will notify Node when the hard disk is ready. This model simplifies access to slow resources in a scalable way that is intuitive and easy to understand. Especially for users who are familiar with DOM events such as onmouseover and onclick, they will feel familiar.

Although letting Javascript run on the server side is not a unique feature of Node, it is one of its powerful features. We have to admit that the browser environment limits our freedom to choose programming languages. Any desire to share code between servers and increasingly complex browser client applications can only be achieved through Javascript. Although there are other platforms that support Javascript running on the server side, Node has developed rapidly and become the de facto platform because of the above characteristics.

15 Nodejs application scenarios

We already have a preliminary understanding of Nodejs, let’s take a look at the application scenarios of Nodejs.

1 Web development: Express EJS Mongoose/MySQL

express is a lightweight and flexible Nodejs web application framework that can quickly build a website. The Express framework is built on the built-in Http module of Nodejs and repackages the Http module to implement the function of Web request processing.

ejs is an embedded Javascript template engine that generates HTML code through compilation.

mongoose is the object model tool of MongoDB. Through the Mongoose framework, you can access MongoDB.

mysql is a communication API that connects to the MySQL database and can be used to access MySQL.

Normally using Nodejs for web development requires the use of three frameworks, just like SSH in Java.

2 REST development: Restify

restify is a REST application framework based on Nodejs, supporting server and client. Restify focuses more on REST services than express, removing the template, render and other functions in express. At the same time, it strengthens the use of REST protocol, versioning support, and HTTP exception handling.

3 Web Chat Room (IM): Express http://Socket.io

socket.io is a software based on the Nodejs architecture and supports the websocket protocol for real-time communication. Bag. Socket.io provides a complete package for building real-time applications across browsers. Socket.io is completely implemented by JavaScript.

4 Web crawler: Cheerio/Request

cheerio is a fast, flexible and encapsulating jQuery core function toolkit specially customized for the server. Cheerio includes a subset of core jQuery, stripping away all DOM inconsistencies and browser incompatibilities from the jQuery library, revealing its truly elegant API. Cheerio works on a very simple and consistent DOM model, making parsing, operation, and rendering incredibly efficient. Basic end-to-end benchmarks show that Cheerio is approximately eight times (8x) faster than JSDOM. Cheerio encapsulates the @FB55 compatible htmlparser and can parse almost any HTML and XML document.

5 Web blog: Hexo

Hexo is a simple, lightweight, static blog framework based on Node. Through Hexo, we can quickly create our own blog, which can be completed with only a few commands.

When released, Hexo can be deployed on your own Node server or on github. For individual users, deploying on GitHub has many benefits. It can not only save the cost of servers, but also reduce the troubles of various system operation and maintenance (system management, backup, network). Therefore, personal sites based on github are beginning to become popular...

6 Web Forum: nodeclub

Node Club is a new community software developed with Node.js and MongoDB, with an elegant interface and powerful functions Rich, compact and fast, it has been used in the Node.js Chinese technical community CNode, but you can use it to build your own community.

7 Web Slideshow: Cleaver

Cleaver can generate presentations based on Markdown. If you already have a Markdown document, you can create a slideshow in 30 seconds. Cleaver is a tool for Hackers.

8 Front-end package management platform: bower.js

Bower is a package management tool launched by twitter. Based on the modular idea of ​​nodejs, the functions are dispersed into various modules, so that the modules and There is a relationship between modules, and Bower is used to manage this relationship between modules.

9 OAuth authentication: Passport

The Passport project is an authentication middleware based on Nodejs. The purpose of Passport is only for "login authentication". Therefore, the code is clean, easy to maintain, and can be easily integrated into other applications. Web applications generally have two forms of login authentication: username and password authentication login, and OAuth authentication login. Passport can configure different authentication mechanisms according to the characteristics of the application. This article will introduce the authentication login of user name and password.

10 Scheduled task tool: later

Later is a tool library based on Nodejs that executes scheduled tasks in the simplest way. Later can run in Node and the browser.

11 Browser environment tools: browserify

The emergence of Browserify allows Nodejs modules to run in the browser, use the require() syntax format to organize the front-end code, and load npm modules. In the browser, the code compiled by calling browserify is also written in the