1. Chinese garbled solve the problem:
the require HTTP = const ( 'HTTP' ) const Server = http.createServer ((REQ, RES) => { // set the character encoding - parsed form of the text // 200 represents this request is successful // text / html ---- resolution tag output // text / plain ---- ---- escape tag output is outputted // nodejs server default text / HTML res.writeHead (200 is , { 'the Type-the Content': 'text / Plain; charset = UTF-. 8 ' }) res.write ( ' <h1 of> Hello World </ h1 of> ' ) res.write ( ' Hello <H2> </ H2> ') // if there is Chinese characters, The default setting will be garbled ---- character encoding res.end () }) server.listen ( 3000 ) console.log ('Runing with your Server IS AT HTTP: // localhost: 3000' ) // ???? open the console - "NETWORK, refresh the page, I found two request is a request to --- ---- not filter 2 requests required.
2. Parse the URL (the URL of your set)
// HTTP // parse URL url / * * * parse the full URL address field ┌────────────────────────────── ────────────────────────────────────────────────── ────────────────┐ │ │ href ├──────────┬──┬─────────────── ──────┬────────────────────────┬────────────────── ─────────┬───────┤ │ │ │ protocol Host auth │ │ path hash │ │ │ │ │ ├─────────────── agreement │ ──┬──────┼──────────┬────────────────┤ │ │ │ │ │ pathname hostname Port │ │ Search │ │ │ │ │ │ │ domain routing port │ │ ├─┬──────────────┤ │ │ │ │ │ │ │ │ │ │ Query "HTTPS: // the User: Pass @ Sub. example.com:? 8080 / the p-/ A / t / H = Query String #hash " │ │ │ │ │ │ │ Port hostname │ │ │ │ │ │ │ ├───────────── ────┴──────┤ │ │ parameter hash value │ │ │ │ Protocol username password │ │ │ │ │ │ Host ├──────────┴──┼─── ───────┴──────────┼────────────────────────┤ │ │ │ Origin │ │ Origin │ │ │ Search pathname hash │ │ ├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤ Href │ │ └────────────────────────────────────────────── ────────────────────────────────────────────────── ┘ * Protocol: 'HTTP:' // protocol* / const url = the require ( 'url' ) const urlStr = "HTTP: // localhost:? 3000 / wudaxun the Login username & password = 123456 =" // url.parse (urlStr [, boolean]) character --- conversion of the URL string type objects into const obj = url.parse (urlStr, to true ) // the console.log (obj) / * * * the Url { host: 'localhost: 3000' // domain name + port port: '3000', // port slashes: to true, auth: null, // Author hostname: 'localhost', // domain hash: null, // hash value search: '? username = wudaxun & password = 123456', // + parameters? Query: 'username = wudaxun & password = 123456 ', // string type argument pathname:' / login ', // routing path:' / login username = wudaxun & password = 123456 ', // routing +?? + Parameters href: 'http: // localhost: ? 3000 / login username = wudaxun & password = 123456' // complete address } * / console.log (obj.query) // username & password = 123456 = wudaxun
console.log(obj.query.username) //wudaxun
console.log(obj.query.password) //123456
3.querystrng converted into a string parameter type of object type
/ * * * QueryString * converted into a string parameter type of object type * * & wudaxun username = password = 123456 * * ===> * * {username: wudaxun, password: 123456} * / const QueryString = the require ( 'QueryString' ) const queryStr = 'username = password = 123456 & wudaxun & Age = 18 is' // querystring.parse (queryStr) * ---- to convert the object into a string type argument const obj = querystring.parse (queryStr) the console.log (obj) / * ** * { username: 'wudaxun', password: '123456', Age: '18 is' } summary: The actual development process does not require the use querystring, recommended url.parse (str, true) second argument url.parse () is true to turn the object, the principle is querystring.parse () * / // ?? ? known pages string can get, so if the user enters the URL of it?
4. resolve URL (the URL of the server to obtain)
const http = require('http') const url = require('url') const server = http.createServer((req, res) => { if (req.url !== '/favicon.ico') { res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8' }) const urlStr = req.url // /login?username=wudaxun&password=123456 const obj = url.parse(urlStr, true).query // { username: 'wudaxun', password: '123456' } const { username, password } = obj res.write ( `user name: $ {username}`) res.write ( `Password: $ {password}`) res.end () } }) server.listen ( 3000 ) // Login & Registration Form Do not use - - loss of data / * * * If the user enters / home then the output Home * If the user input / kind then output classification * If the user input / cart shopping cart then output * If the user input / user then the output of individual centers * /
fs(file-system)
/ * * * FS File System ---- * flag rear language: * write files * / // read the file // fs.readFile (path, 'UTF-. 8', (ERR, Data) => {}) const FS = the require ( 'FS' ) fs.readFile ( './01node introduced .html', 'UTF-. 8', (ERR, Data) => { IF (ERR) { the console.log ( ERR) } the else { the console.log (Data) } })
Open another web page on the server:
the require HTTP = const ( 'HTTP' ) const FS = the require ( 'FS' ) const Server = http.createServer ((REQ, RES) => { IF (req.url! == '/favicon.ico' ) { RES .writeHead ( 200 is , { 'the Type-the Content': 'text / HTML; charset = UTF-. 8' }) fs.readFile ( './12test.html', 'UTF-. 8', (ERR, Data) => { IF (ERR) the throw ERR // encounters an error, an exception is thrown, code is no longer continue res.write (the Data) res.end () }) } }) Server.listen(3000) / * * * If the user enters / home then the output Home .html * If the user input / kind classify the output .html * If the user input / cart shopping cart then output .html * If the user input / user then the output of individual centers .html * /