Saturday 16 February 2019

node.js: express module tutorial

HelloWorld application
Middleware function
Middle ware function that executes on every request
How can you execute a middleware function for every request that comes to the server?
Throwing exceptions
Send static files
Routing
Express and EJS helloWorld application
NODE_ENV: Working with different environments
index.js file
Express: Exploring express.Router([options])
Express: handling favicon
Express: Create custom error page
Exploring express application
     app.locals: Set Application level data
     app.mountPath: Gives the patterns on which this sub app is mounted
     mount event: Get the event when the app is mounted
     app.all(path, callback [, callback ...]): Match to all http verbs
     app.delete(path, callback [, callback ...]): Route delete requests
     app.set(name, value): Set application property
     app.get(name): Return the value associated with this property
     app.disable(name): Disable the boolean property
     app.disabled(name): Return true if this property is disabled
     app.enable(name): Enable the property
     app.enabled(name): Returns true if the setting name is enabled
     app.engine(ext, callback): Register template engine
     app.get(path, callback [, callback ...]): Process get request
     app.listen([port[, host[, backlog]]][, callback]): Listen application on this port
     app.METHOD(path, callback [, callback ...]): Route http request to specific method
      Route parameters
     app.param([name], callback): Add callbacks to the route parameters
     app.path(): Get canonical path of the application
     app.post(path, callback [, callback ...]): Route post requests
     app.put(path, callback [, callback ...]): Route put requests
     app.route(path): Return the router instance for this path
     app.use([path,] callback [, callback...]): Mount middleware functions to this path
     Error handling middleware
Exploring request object
     req.app: Get the reference of express application
     req.baseUrl: Return the url path on which this router instance is mounted
     req.originalUrl: Return original request url
     req.path: return the path part of the url
     req.body: Get the request payload
     req.cookies: Get the cookies associated with this request
     req.method: Get the http method of the request
     req.params: Get route parameters
     req.protocol: Get the request protocol: http or https
     req.query: Get the query parameters
     req.route: Get currently matched route
     req.secure: Tells about secure connection
     req.accepts(types): Check whether given content types are accepted by client or not
     req.acceptsCharsets(charset [, ...]): Return the accepted char sets of the client.
     req.acceptsEncodings(encoding [, ...]): Get the accepted encodings by client
     req.acceptsLanguages(lang [, ...]): Return the accepted language by client
     req.get(field): Access http request headers
     req.is(type): Check the matching content type
     req.param(name [, defaultValue]): Return value of the parameter
Exploring response object
     res.app: get reference to express application instance
     res.headersSent: Check whether response headers send to the client or not
     res.locals: Set response local variables
     res.append(field [, value]): Append response header
     res.attachment([filename]): Set Content-Disposition header
     res.cookie(name, value [, options]): Set cookie name to value
     res.clearCookie(name [, options]): Clears the cookie with this name
     res.download(path [, filename] [, options] [, fn]): Download the file at given path
     res.end([data] [, encoding]): End the response process
     res.format(object): perform content negotiation on Accept http header
     res.set(field [, value]): Set http response header field
     res.get(field): Get http response header field
     res.json([body]): Send json response
     res.links(links): Populate response Link http header field
     res.location(path): Set location header
     res.redirect([status,] path): Redirect the url
     res.render(view [, locals] [, callback])
     res.send([body]): Send http response
     res.sendFile(path [, options] [, fn]): Send file at given path
     res.sendStatus(statusCode): Send response code
     res.status(code): Set the status code for the response
     res.type(type): Set Content-Type header
How can you detect whether a file is directly run from node or not?
How to debug node.js Application?


Previous                                                 Next                                                 Home

No comments:

Post a Comment