English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Node Learning Record: Tutorial for Building Web Server

Basic knowledge of web servers

Function:1. Receive HTTP requests (get, post, delete, put)2. Process HTTP requests

Common web server architecture:

1. Nginx/Apache: responsible for receiving http requests, determining who handles the request, and returning the result of the request

2. php-fpm/php module

Common requests

1Request files: including static files to be processed

2To complete specific operations, such as login, get specific data, etc.

Use http to create a web server

//Introduce the core module http of nodejs
var http = require('http') ;
//Create an http instance
var reqHandler = function(req,res){
 res.end("I have received all") //Used to indicate that all the response content has been received
}
var web = http.createServer(reqHandler());
web.listen(666)
console.log("http runnibg on http:"//localhost:666")

Run method, input node httptest.js in the command line

Use express to create a web server

//Introduce modules
var express = require("express") ;
var app = express() ;
//Control the response through routing;
app.get('/',function(req,res){
 res.end("mingming love dama")

app.listen(666 , function(){
 console.log('express is running')
}) ;

Same effect as above

Static files

app.use(express.static('./public'))

With ./public as the starting position of the static file repository.

Routing

Allocate different requests to the response handling functions

Three methods of routing :1) path method  2)Router method  3)route

1. path method

app.get('/test',function(req,res){
 res.send("id: ")+req.query.id+" password: ":"}}+req.query.password);

When we request http://localhost:3000/test?id=120&password=11001when

  

This is the path method to set up the route.

2. Router method 

var express = require("express") ;
var app = express();
app.use(express.static('./public'))
//Import the route
var router = express.Router() ;
router.use(function log(){
 console.log('Time: ', Date.now());

//Call the route
router.get('/',function(req,res){
 res.send('Birds home page');

router.get('/about',function(req,res){
 res.send('Birds about page')

router.get('/test',function(req,res){
 res.send('Birds test page')

app.use('/bird',router)
app.listen(3000,function(){
 console.log("express is running")
 //The callback method for successful listening

This is how the route is customized

That's all for this article. Hope it helps everyone's learning and also hope everyone will support the Yelling Tutorial more.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit manually, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like