callback / asnyc
|
|
the async fun take the callback fun as its last parameter.
event loop
|
|
event emitter
when async IO is done, will send an event to the Event queue. e.g. when fs.readStream() open a file will trig an event. e.t.c
|
|
file system
|
|
buffer
as js language has only txt bytes data, to deal with binary data, introduce Buffer
|
|
stream
|
|
module system
to enable different nodejs files can use each other, there is a module system, the module can be a nodejs file, or JSON, or compiled C/C++ code.
nodejs has exports
and require
used to export modules’ APIs to external usage, or access external APIs.
|
|
the first way export the object itself, the second way only export the certain method.
Global Object
|
|
common modules
- path
|
|
- http server
|
|
- http client
|
|
Express
Express has requst
and response
object to handle request and reponse. express.static
can handle static resources, e.g. image, css e.t.c
|
|
res
is what send from server to client, for both /get
, /post
methods. req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers e.t.c
- req.body
contains key-value pairs of data submitted in the request body. by default, it’s undefined, and is populated when using body-parsing middleware. e.g. body-parser
- req.cookies
when using cookie-parser middleware, this property is an object that contains cookies send by the request
- req.path
contains the path part of the request url
- req.query
an object containing a property for each query string parameter in the route
- req.route
the current mathced route, a string
data access object(DAO)
Dao pattern is used to separate low level data accessing API or operations form high level business services. usually there are three parts:
DAO interface, which defines the standard operations to be performed on a model object
DAO class, the class that implement DAO interfaces, this class is responsible to get data from database, or other storage mechanism
model object, a simple POJO containing get/set methods to store data retrieved using DAO class
o/r mapping (orm) is used a lot to map database itme to a special class, and it’s easy to use, but a little drawback of orm is it assume the database is normalized well. DAO is a middleware to do directly SQL mapping, who mapes SQL query language to the output class.
separting models, logic and daos
routes.js, where to put routes, usually referenced as
controllers
models.js, where to put functions talk to database, usually referenced as
dao layer
views.js
these three components can put under app; all static data usually put under public
folder; the Express package.json
and index.js
are at the same level as app
.