Uploaded on Oct 17, 2019
Web servers are software products that use the operating system to handle web requests. These requests are redirected to other software products (ASP.NET, PHP, etc.), depending on the web server settings.
Complete Node.js Web Server Tutorial with Example
NiFooduer.j Cs oWnesubl Staenrvceyr
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Table of Contents
The Web Server
The Request Object
The Response Object
Routing Requests
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
2
What Do the Web Servers Do? (Again)
All physical servers have hardware
The hardware is controlled by the operating system
Web servers are software products that use the operating system to handle web
requests
Web servers serve Web content
These requests are redirected to other software products (ASP.NET, PHP, etc.),
depending on the web server settings
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
3
NodeJS Web Server
Require the 'http' module
Create server function (createServer)
Request/Response wrapper objects
Listen function (specifies port and IP (host))
Headers are objects (keys are lowercased)
{
'content-length': '123',
'content-type': 'text/plain',
'connection': 'keep-alive',
'accept': '*/*'
}
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
NodeJS Web Server
Basic server implementation
http://nodejs.org/api/http.html
let http = require('http')
http
.createServer((req, res) => {
res.writeHead(200, {
'Content-Type': 'text/plain'
}) // return success header
res.write('My server is running! ^_^') // response
res.end() // finish processing current request
})
.listen(1234);
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
The Request Wrapper
The Request wrapper
http.IncommingMessage class
Implements the Readable Stream interface
Properties
httpVersion – '1.1' or '1.0'
headers – object for request headers
method – 'GET', 'POST', etc.
url – the URL of the request
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
The Response Wrapper
The Response wrapper
http.ServerResponse class
Implements the Writable Stream interface
Methods
writeHead(statusCode, [headers])
let body = 'hello world'
res.writeHead(200, {
'Content-Length': body.length, // not always valid
'Content-Type': 'text/plain',
'Set-Cookie': ['type=master', 'language=javascript']
});
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
The Response Wrapper
Methods
write(chunk, [encoding])
end()
Always call the methods in the following way
writeHead
write
end
res.writeHead('Hello world!') // default encoding: utf8
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Route Requests
URL Parsing modules
'url' and 'querystring'
both have parse method
let url = require('url')
console.log(url.parse('/status?name=ryan'))
// logs
// {
// href: '/status?name=ryan',
// search: '?name=ryan',
// query: { name: 'ryan' },
// pathname: '/status'
// }
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Comments