当前位置:网站首页>Nodejs notes 2
Nodejs notes 2
2022-04-22 20:57:00 【Revin050】
List of articles
http modular
What is? http modular
In a network node , Computers responsible for consuming resources , It's called a client ; The computer responsible for providing network resources to the outside world , It's called a server .
http The module is Node.js Official 、 Used to create web Module of the server . adopt http Module provided http.createServer() Method , Just
Can easily put an ordinary computer , Become a Web The server , So as to provide Web Resource service .
http Role of module
The difference between a server and an ordinary computer is , There is... Installed on the server web Server software , for example :IIS、Apache etc. . By installing these server software ,
Can turn an ordinary computer into a web The server .
stay Node.js in , We don't need to use IIS、Apache Wait for these third parties web Server software . Because we can be based on Node.js Provided
http modular , Through a few lines of simple code , You can easily write a server software , So as to provide web service .
Server related concepts
IP Address
IP Address is the unique address of every computer on the Internet , therefore IP The address is unique . If you put “ Personal computer ” Compared to the “ A phone ”, that “IP The earth
site ” Equivalent to “ Phone number ”, Only when you know each other IP On the premise of address , To communicate with the corresponding computer .
IP Format of address : Usually use “ dotted decimal ” Expressed as (a.b.c.d) In the form of , among ,a,b,c,d All are 0~255 A decimal integer between . for example : use
Dotted decimal IP Address (192.168.1.1)
Be careful :
① Every computer on the Internet Web The server , All have their own IP Address , for example : You can Windows Running in the terminal ping www.baidu.com life
Make , You can view the of Baidu server IP Address .
② During development , Your computer is both a server , It's also a client , For testing purposes , You can enter... In your browser 127.0.0.1 This
IP Address , You can access your computer as a server .
Domain names and domain name servers
Even though IP An address can uniquely label a computer on a network , but IP The address is a long string of numbers , Is not intuitive , And it's not easy to remember , So people invented another set
Character type address scheme , The so-called domain name (Domain Name) Address .
IP Address and domain name are one-to-one correspondence , This correspondence is stored in a server called a domain name server (DNS,Domain name server) In my computer . Users
Just visit the corresponding server through a friendly domain name , The corresponding conversion is realized by the domain name server . therefore , Domain name server is to provide IP Address and domain name
Between the conversion services of the server .
Be careful :
① Just use IP Address , Computers on the Internet can also work properly . But with the blessing of the domain name , Can make the world of the Internet more convenient .
② During development testing , 127.0.0.1 The corresponding domain name is localhost, They all represent our own computer , There is no difference in the use effect .
Port number
Port number in the computer , It's like a house number in real life . Pass the house number , Takeout boy can be in many rooms in the whole building , Accurately take out
To your hand .
Same thing , In a computer , Can run hundreds of web service . Every web Each service corresponds to a unique port number . From the client
Network request , Through port number , Can be accurately handed over to the corresponding web Service processing .
Be careful :
① Each port number cannot be multiple at the same time web Service occupancy .
② in application ,URL Medium 80 Ports can be omitted .
Create the most basic web The server
Create a server
There are four steps to creating a server
1.- Import http modular
2.- establish web Server instance
3.- Bind to the server instance request event
4. - Start the server

req Request object
As long as the server receives the client's request , The call will pass server.on() Bound to the server request Event handler .
If you want to in the event handler , Access data or properties related to the client , You can use the following ways :


res The response object
On the server request In the event handler , If you want to access data or properties related to the server , You can use the following ways :



Solve the problem of Chinese garbled code
When calling res.end() Method , When sending Chinese content to the client , There will be a mess , here , You need to manually set the encoding format of the content :
Before setting, it is shown as follows


After setting, it is shown as follows


According to url Respond to different html Content
The implementation idea is as follows
① Obtain requested url Address
② Set the default response content to 404 Not found
③ Judge whether the requested by the user is / or /index.html home page
④ Judge whether the requested by the user is /about.html About the page
⑤ Set up Content-Type Response head , Prevent Chinese miscoding
⑥ Use res.end() Respond the content to the client
according to url Address , Show different content .
The specific code is as follows
const http = require('http')
const server = http.createServer()
server.on('request', (req, res) => {
// 1. Obtain requested url Address
const url = req.url
// 2. Set the default response content to 404 Not found
let content = '<h1>404 Not found!</h1>'
// 3. Judge whether the requested by the user is / or /index.html home page
// 4. Judge whether the requested by the user is /about.html About the page
if (url === '/' || url === '/index.html') {
content = '<h1> home page </h1>'
} else if (url === '/about.html') {
content = '<h1> About the page </h1>'
}
// 5. Set up Content-Type Response head , Prevent Chinese miscoding
res.setHeader('Content-Type', 'text/html; charset=utf-8')
// 6. Use res.end() Respond the content to the client
res.end(content)
})
server.listen(80, () => {
console.log('server running at http://127.0.0.1')
})
The effect is as follows



版权声明
本文为[Revin050]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222056562269.html
边栏推荐
- 1、 Machine learning concepts
- Use of list
- Comparison between Chinese and American programmers: do you agree
- 剑指offer汇总
- Implementation of simple FreeRTOS task blocking delay based on gd32f1x0 manual programming
- MySQL storage engine
- Website: fakeimg Pl (text -- > picture)
- 基于PAOGD_HW1的弹出的小球-简单建模、插值动画
- 面试官宁愿要刚刚毕业工作1年的我小弟,也不要工作5年的我,年薪25w
- October's Android interview failed miserably in byte three, and fortunately won Xiaomi offer
猜你喜欢
![[interview ordinary people vs Expert Series] please talk about the network quadruple](/img/66/e83bf24095b506a8de68166d7b3bec.png)
[interview ordinary people vs Expert Series] please talk about the network quadruple

MySQL高可用架构设计分析

联想电脑管家图文介绍:联想电脑管家怎么下载?
![buuctf-[Flask]SSTI](/img/62/f272848dd18b75fd837621458b573c.png)
buuctf-[Flask]SSTI

String. Join() and stringutils Join () gracefully solves the splicing of arrays or collections

容联云携手中国信通院,开启办公即时通信软件系列标准研制

After working for three years as a programmer in a small 9K factory with a monthly salary, he finally won the byte offer he had been longing for for for for a long time in his spare time. The salary c

基于PAOGD_HW1的弹出的小球-简单建模、插值动画

第一章 虚拟现实技术概论
![buuctf-[Flask]SSTI](/img/62/f272848dd18b75fd837621458b573c.png)
buuctf-[Flask]SSTI
随机推荐
Building a new generation of computing platform, stepvr will open the "door" of metauniverse in 2022
蜥蜴书学习day1-机器学习概述
Error running ‘JeecgSystemApplication‘: Command line is too long. Shorten command line for JeecgSyst
Active mode and passive mode of FTP
The traffic not included in the Internet era is all included in the industrial Internet era
- 4. Compare strings (10 points) the C language standard function library includes the StrCmp function for string comparison. As an exercise, we write a function with the same function.
Is it safe for futures to open an account directly online?
联想电脑管家图文介绍:联想电脑管家怎么下载?
Application value of smart agriculture app software
Who is important about products and services? Changan Ford tells you "all"
华为机试题——HJ72 百钱买百鸡问题
88 R 用户画像之线性回归逻辑回归综合实战 1
UnityShader入门精要——素描效果渲染
Leetcode222. Number of nodes of complete binary tree
After working for three years as a programmer in a small 9K factory with a monthly salary, he finally won the byte offer he had been longing for for for for a long time in his spare time. The salary c
Programming utilities, there is always one you use
String. Join() and stringutils Join () gracefully solves the splicing of arrays or collections
基于SEIR模型的传染病预测软件开发
[interview ordinary people vs Expert Series] please talk about the network quadruple
2022年G3锅炉水处理国家题库及在线模拟考试