The article directories
-
-
-
- Node create server [HTTP module]
- Buffer & Stream
-
- Read/write data stream
- Pipe event [PIPE]
- Write a funny one
- Local services render Html as well as JSON
-
- Rendering Html
- Rendering JSON
- npm
-
- package.json
-
- The installation package. Json
- node_modules
- Uninstalling third-party libraries [NPM uninstall]
- Document third-party libraries in package.json
-
- npm install jquery –save
- npm install jquery –save-dev
- npm install jquery
- later
-
-
Node create server [HTTP module]
- The client sends a request to the server, and the server directly respons the request to the client. In fact, this is to send an HTTP or HTTPS request to the server. Then we connect with socket in the middle, and finally the server sends back what we need through Tcp.
- Create local server (HTTP)
/ * * *@author clearlove
* @aim Demonstrates setting up the server locally */
/ / incoming HTTP
var http = require('http');
// Create a server
const server = http.createServer(function (req, res) {
console.info('The client sends a request to the server${req.url}`)
// Receive the message from the server
res.writeHead(200, {"Content-type":"text/plain"})
res.end('server is working... ')})// Listen for requests on local port 8888
server.listen(8888.'127.0.0.1');
// Print directly when running the js
console.info('server is running... ')
Copy the code
Here we listen for port 8888 and type a message from the server when it is in use
- Let’s go straight to:
- Let’s access my local address + port
Ok, no problem, the local service is running, of course we can return json data or HTML pages.
Buffer & Stream
- Buffer is designed to handle binary streams on the server side in scenarios such as TCP streams and file system operations
- Stream is an abstract interface that processes Stream data in NodeJS
A buffer is a box that buffers bits of data and stores them, but if you want to Stream data to a page through a Stream, it’s probably better to say that. In fact, there is a pipe event in the middle of this process, which is not mentioned here, but more confusing
Read/write data stream
Look at the code:
/ * * *@author clearlove
* @aim Demonstrates reading and writing data streams */
/ / introduction of the fs
var fs = require('fs')
const myreadstream = fs.createReadStream(__dirname + '/readMe.txt'.'utf-8')
// Write to the file stream
const mywritestream = fs.createWriteStream(__dirname + '/writeMe.txt')
// If you print myReadStream, it returns an object, not the content of our implementation
//console.info(myreadstream)
// Display the content
var timer = 0;
myreadstream.on('data'.function (chunk) {
timer++
console.info('============= received${timer}Partial data ============= ')
mywritestream.write(chunk)
})
// The result of the run is: parts of the display (if you have enough data content)
Copy the code
Running results:
Writeme. TXT is our readme.txt, so you can run it yourself.
Pipe event [PIPE]
- Here or ask the pipeline event, though they may be a bit dizzy, but this is more useful, the previous example we saw, we according to the flow of the contents of a file which way a little bit of writing to a new file inside, but using pipeline, is actually very simple. A pipe event is something similar to a pipe, one end of the pipe is the entrance and the other end is the exit, the pipe is not necessarily straight, may be bent, no matter what shape, the ultimate purpose is one end in and one end out, so it may be nonsense to say.
- Look at the code:
/ * * *@author clearlove
* @aim Demonstrates pipeline events reading and writing data streams */
/ / introduction of the fs
var fs = require('fs')
const myreadstream = fs.createReadStream(__dirname + '/readMe.txt'.'utf-8')
// Write to the file stream
const mywritestream = fs.createWriteStream(__dirname + '/writeMe2.txt')
// Use pipe events
myreadstream.pipe(mywritestream)
Copy the code
This will write the content directly to another file named writeme2.txt. Let’s take a look at the document:
I’m not going to open the writeme2. TXT file, it’s a little bit too much, and it’s not interesting to see in the screenshots. You can just run it yourself.
Write a funny one
Now that we know how to create a server, now that we know how to read files, now that we know how to pipe events, let’s write an effect, let’s set up a server, let’s write a local text, let’s export the local text to the page, okay
- Look at the code:
/ * * *@author clearlove
* @aim Make a little thing */
/ / introduction of the fs
var fs = require('fs')
var http = require('http')
/** * Set up a server */
var server = http.createServer(function (res,res) {
res.writeHead(200, {"Content-type" : "text/plain"})
const myreadstream = fs.createReadStream(__dirname + '/readMe.txt'.'utf-8')
myreadstream.pipe(res)
})
server.listen(8888.'127.0.0.1');
console.info("server is running...")
Copy the code
See the effect:
Local services render Html as well as JSON
Rendering Html
- Let’s make a simple page here, then run our own service, run it, render our page to our local, look at the code:
<! DOCTYPEhtml>
<html>
<head>
<meta charset="utf-8">
<title>http_demo</title>
<style>
body{
background: #307ac6;
text-align: center;
color: aliceblue;
margin: 10% 10%
}
p{
font-size: 2rem
}
</style>
</head>
<body>
<h1>
WelCome to CSDN of clearlove
</h1>
<p>
If you like my article, you can follow my blog
</p>
</body>
</html>
Copy the code
/ * * *@author clearlove
* @aim Make a little thing */
/ / introduction of the fs
var fs = require('fs')
var http = require('http')
/** * Set up a server */
var server = http.createServer(function (res, res) {
/** * this handles the case of /favicon.ico for the second request */
if(res.url ! = ='/favicon.ico') {
res.writeHead(200, { "Content-type": "text/html" })
const myreadstream = fs.createReadStream(__dirname + '/views/http_demo.html'.'utf-8')
myreadstream.pipe(res)
}
})
server.listen(8888.'127.0.0.1');
console.info("server is running...")
Copy the code
- Start the server and run:
Rendering JSON
- In order to be realistic, I will directly take a JSON data rendering that I used and look at the code:
/ * * *@author clearlove
* @aim Make a little thing */
/ / introduction of the fs
var fs = require('fs')
var http = require('http')
/** * Set up a server */
var server = http.createServer(function (res,res) {
res.writeHead(200, {"Content-type" : "application/json"})
const myreadstream = fs.createReadStream(__dirname + '/data/demo.json'.'utf-8')
myreadstream.pipe(res)
})
server.listen(8888.'127.0.0.1');
console.info("server is running...")
Copy the code
– Run and see the result:
Garbled code on the garbled code, also do not know How Google now so many things. Json data and obj data are essentially the same, but they are very similar. Many people think they are the same thing. Obj can be a key without quotation marks, and a value can be a single quotation mark. I want to emphasize that. Of course, you can render it in HTML or plain, which is text, and you can render it.
npm
- Node Package Manage is a package management tool. In fact, I have written about this in many places in my blog when I wrote about Vue in the past, so I will not elaborate too much here. It is just a simple installation tool, but I will introduce its core thing, Package
package.json
- The package is used to define the modules required for the project, as well as the configuration information of the project (such as the name, version, license, etc.). The name version is required
As a patriotic person like me, I usually refuse to see this language, but I can’t help it in order to learn it. It means that if you want to create a package file, the name and version are required, or you should stop using it.
The installation package. Json
- npm init
Instead, just look at the picture:
To explain the black box problem, I wrote the tutorial on a MAC before, and then I had to write the project on a MAC in the middle, so if I wanted to continue to write the tutorial, I had to write it on my Windows, so the bar was not so high, but the effect and result were the same, so don’t worry about these details. – Now that we have package.json we can directly install what we want. Let’s take an example: install third-party JS
- Install jquery into our project
- npm install jquery
– Here someone said, how do I know the command ah, so many JS files, a lot of ways, the fastest way is to ask me, of course, is impossible, the fastest way is Baidu, followed by their own direct to the official website to find this JS installation method - How do I find it?
Find the JS installation command you need
We just open what we need
Put a link and click on it to see for yourself
First is to introduce a lot of said that this JS how good, how fast, how portable (useless), there is a node to introduce the following, which is his installation method, OK no nonsense
node_modules
- Node_modules: node_modules: node_modules: node_modules: node_modules: node_modules
As you can see, there is a jquery file inside, so you can use jquery directly.
Uninstalling third-party libraries [NPM uninstall]
- NPM uninstall Library name
Document third-party libraries in package.json
- NPM install jquery –save jquery — NPM install jquery –save jquery In fact, both are correct, except that the end of the previous installation will not be recorded in our package, while the latter will be recorded.
- To demonstrate this, first we uninstalled jquery, then we installed it in a different way
npm install jquery –save
{
"name": "nodelearn"."version": "1.0.0"."description": "this is nodelearnpro"."main": "node_demo.js"."scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "clearlove"."license": "ISC"."dependencies": {
"jquery": "^ 3.4.1 track"}}Copy the code
npm install jquery –save-dev
{
"name": "nodelearn"."version": "1.0.0"."description": "this is nodelearnpro"."main": "node_demo.js"."scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "clearlove"."license": "ISC"."dependencies": {},
"devDependencies": {
"jquery": "^ 3.4.1 track"}}Copy the code
npm install jquery
{
"name": "nodelearn"."version": "1.0.0"."description": "this is nodelearnpro"."main": "node_demo.js"."scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "clearlove"."license": "ISC"."dependencies": {},
"devDependencies": {}}Copy the code
There are two more files in your file, the first one is to record our third-party JS, and the last one is to record the third-party JS from the developer’s point of view. Most of the files here are because I installed it before, and after uninstalling it, it will be empty, but the key is still there, so there is no need to explain the two above.
later
This article is written here, combing knowledge really very tired, I’ll update the rest of the next article about communication, introduce how to use nodejs ran a local service, write a simple communication, can look like, the above write wrong, also want to contact me, I correct in time, email, thank you!