1, install,
- A lot of users
- Suitable for high concurrency scenarios
- Many functions can be realized
- Install a stable Node version
- Node -v View the version
- Write a sentence in app.js and print it out using Node app.js
2. HTTP module URL module
- PHP requires an APACHE Nginx Http server
- Nodejs without
- Install a node SN plug-in
- Once installed, tap a Node to get out
Var HTTP = require(" HTTP "); Http.createserver (function(req,res){// Set the response header res.writehead (200,{" content-type ":"text/plain"}); Res.end ("Hello world"); }).listen(8081); // Monitor port console.log('Server running at http://127.0.0.1:8081/')Copy the code
- The node app. Js
- http://127.0.0.1:8081/ Prints out the content
Changes to the code take effect only after the service is restarted
- The current
Chinese characters will be garbled on the page
? How to solve it?
res.writeHead(200,{"Content-Type":"text/plain; charset='utf-8'"}); Res.write ("<head><meta charset=' utF-8 '></head>");Copy the code
- However, the reality is set up such a page or garbled no effect how to deal with?
- How to get the page
The Url?
Just use req.url
Console. log(req.url) // Obtain the page URLCopy the code
- So how do I get it
Name =zhangsan & age =20 only the data on the right
The core of url module learning is that url.parse() can get the content after url splitting
const { get } = require("http"); // introduce HTTP const url = require("url"); Var API = "http://www.baidu.com?name=zhangsan&age=20"; // console.log(url.parse(api,true)) var getValue = url.parse(api,true).query; Console. log(getValue) console.log(' Name :${getValue.name} -- age :${getValue.age} ')Copy the code
- In the original app.js; How do it
To get the data
Var HTTP = require(" HTTP "); Const url = require("url") // Request and response http.createserver (function(req,res) res.writeHead(200,{"Content-Type":"text/html; charset='utf-8'"}); Res.write ("<head><meta charset=' utF-8 '></head>"); Console. log(req.url) // Get the page URL if(req.url! = '/favicon.ico'){ var userinfo = url.parse(req.url,true).query; Console. log(' Name :${userinfo.name}-- age ${userinfo.age} ')} res.end("Hello world Hello "); }).listen(8081); // Monitor port console.log('Server running at http://127.0.0.1:8081/')Copy the code
- The page is tested like this