Building a Web server using Node is very easy
- Node provides a single core module: HTTP
- The HTTP module is responsible for helping you create authoring servers
- Load HTTP core module, HTTP is a variable name, can be customized
var http = require('http')
Copy the code
- use
http.createServer()
Method to create a Web server
Return a Server instance
var server = http.createServer()
Copy the code
- What does the server do?
- Providing services: Services to data
- Send the request
- Receiving a request
- Handle the request
- Give feedback (send a response)
- Register the Request request event
When the client request comes in, the server’s request event is automatically triggered and the second argument is executed: the callback handler
server.on('request'.function () {
console.log('Received client request')})Copy the code
- Bind the port number and start the server
server.listen(3000.function () {
console.log('The server started successfully and can be accessed at http://127.0.0.1:3000/')})Copy the code
- Perform:
- Access:
http://127.0.0.1:3000/
Each access adds a record
HTTP requests and responses
var http = require('http')
var server = http.createServer()
Copy the code
-
Request Request event handler that takes two arguments:
- Request A Request object can be used to obtain information about a client’s Request, such as the Request path
- Response Response object The Response object can be used to send Response messages to clients
server.on('request'.function (request, response) {
// http://127.0.0.1:3000/ result :/
// http://127.0.0.1:3000/a result :/ a
Results: / / http://127.0.0.1:3000/foo/b/foo/b
console.log('Received the request from the client. The request path is:' + request.url)
The response object has a method: write can be used to send response data to the client
// Write can be used multiple times, but the response must be terminated with end, otherwise the client will wait
response.write('hello')
response.write(' nodejs')
// Tell the client that my words are finished and you can present them to the user
response.end()
// Since our server is currently very weak, no matter what request, can only respond to hello nodejs
/ / thinking:
// I want different results when requesting different paths
/ / such as:
// / index
/ / / login
/ / / register registration
// /haha
})
server.listen(3000.function () {
console.log('The server started successfully and can be accessed at http://127.0.0.1:3000/')})Copy the code
- No matter what path is requested, the result is:
hello nodejs
Send different response results based on different request paths
var http = require('http')
1. Create a Server
var server = http.createServer()
// 2. Listen to the request event and set the request handler
server.on('request'.function (req, res) {
console.log('Request received, request path:' + req.url)
console.log('Request my client's address is:', req.socket.remoteAddress, req.socket.remotePort)
// res.write('hello')
// res.write(' world')
// res.end()
// The above method is cumbersome, recommend to use a simpler way, directly end and send the response data
// res.end('hello nodejs')
// Send different response results according to different request paths
// 1. Obtain the request path
// req.url retrieves the portion of the path after the port number
// All urls start with a /
// 2. Determine the path processing response
var url = req.url
if (url === '/') {
res.end('index page')}else if (url === '/login') {
res.end('login page')}else if (url === '/products') {
var products = [{
name: 'apple'.price: 8888
},
{
name: Pineapple 'X'.price: 5000
},
{
name: 'Pepper X'.price: 1999}]// The response content can only be binary data or a string
/ / digital
/ / object
/ / array
/ / a Boolean value
res.end(JSON.stringify(products))
// Convert an array to a string
} else {
res.end('404 Not Found.')}})// 3. Bind the port number to start the service
server.listen(3000.function () {
console.log('Server started successfully, accessible... ')})Copy the code