1. Learn to return different data according to different request paths: My port number: 3000, URL: http://127.0.0.1:3000
var http = require("http"); // http module http.createServer(function(req, res) { //res.write('hello') //res.write('world!') // res.end('index page'); var url=req.url //Get req.url value if(url==='/'){ res.end('index page') //Content ends}else if(url==='/login') { res.end('login page') }else{ res.end('404') } console.log(req.url); }).listen(3000); // Listen to port 3000 console.log("HTTP server is listening at port 3000. The URL is http://127.0.0.1:3000"); result: 2. Data sent: data type and encoding: Content-Type res.end('<p>Who am I<a>Click</a></p>') //Use text/html to be recognized by the browser 3. About reading files: relative path and absolute path:This relative path is actually relative to the path where the node command is executed: var http = require("http"); // http module var fs = require("fs") //var url=req.url; http.createServer(function(req, res) { //res.write('hello') //res.write('world!') // res.end('index page'); fs.readFile('./07.html',function(err,data) { if(err){ res.setHeader('Content-Type','text/plain; charset=utf-8') res.end('wss') } else{ res.setHeader('Content-Type','text/html; charset=utf-8') res.end(data) } }) }).listen(3000); console.log("service") result: 4. Read pictures This concludes this article about the detailed process of node.js returning different data according to different request paths. For more relevant node.js request paths and data content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to deploy LNMP architecture in docker
>>: UTF-8 and GB2312 web encoding
Table of contents Missing root location Off-By-Sl...
This article shares the installation tutorial of ...
Result:Implementation code: <!DOCTYPE html>...
Table of contents Install: 1. Basic use of firewa...
This article tests the environment: CentOS 7 64-b...
Table of contents The origin of JSBridge The bidi...
Table of contents 1. Retrieve via --skip-grant-ta...
This article example shares the specific code of ...
1. How do I remove the blank space of a few pixels...
Today, I set up a newly purchased Alibaba Cloud E...
This article shares the specific code of JavaScri...
Table of contents Case Study Update account balan...
Use js to control the light switch for your refer...
The development of Docker technology provides a m...
Table of contents Seconds_Behind_Master Original ...