Please enable JavaScript.
Coggle requires JavaScript to display documents.
Node Js - Handle Web Requests (1. Create Web Server (Methods…
Node Js - Handle Web Requests
1. Create Web Server
Why?
will handle all the http requests for the web application
can retrieve data from the database
What?
can process the requests asynchronously with help of
http module
allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP)
creates a HTTP server that listens to specified server ports
Methods
require()
listen()
createServer()
write()
end()
request.url()
Objects
Response Object
response.writeHead(statusCode, [headers])
response.end()
response.write("Message")
2. Create Module
Modularize Application
Why?
Easier to debug
Readability
Reusable Code
Reliability
What?
software design technique
functionality of a program is separated into independent modules
module contains a desired functionality
4. Handle POST Requests
Built-in Modules
EventEmitter
querystring
Methods (data, on, end, emit)
querystring.parse()
Built-in Events
request
response
3. Handle GET Request
Built-in Modules
Objects
Exports Object
request.url
url.parse(request.url).query
querystring.parse(query)["parametername"]
url
querystring
5. Route Request
Why?
navigate to different urls based on the incoming data
How?
Conditiong
request.url property
# Custom Events
Extending Event Emitter object