Nodejs with Learning (miscellaneous)

Source: Internet
Author: User

1. Environment custom configuration file and node_env setting

Environment customization refers to drawing different profiles based on the node_env configuration, development and test environment.

Recommended module: Config-lite, use reference: Configuration using in different environments (1190000010099383)

Where the settings in Windows configuration node_env are not in effect,

Recommended module: CROSS-ENV, use reference: Use CROSS-ENV to solve cross-platform settings node_env problems (1190000005811347)

2. Create an HTTP server set of Request objects

"Req": {    "url": "/performance-now.js.map",    "Headers": {      "Host": "localhost:3000",      "Connection": "Keep-alive",      "Cache-control": "No-cache",      "User-agent": "mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/65.0.3325.181 safari/537.36 ",      "Accept-encoding": "gzip, deflate, BR",      "Accept-language": "Zh-cn,zh;q=0.9,ar;q=0.8,cs;q=0.7,de;q=0.6,en;q=0.5,es;q=0.4,fr;q=0.3,gl;q=0.2,hu;q=0.1,id;q =0.1,it;q=0.1,ja;q=0.1,ko;q=0.1,ms;q=0.1,pl;q=0.1,pt;q=0.1,ru;q=0.1,th;q=0.1,tr;q=0.1,vi;q=0.1,zh-tw;q=0.1 ",      "Cookie": "Myblog=s%3ave5neyqvgvihgsoiu_mrnypeyqjvd7bs.qpxxkhewlazj68cn0mznqth6sgsne2graxtrbatparu; blog-demo=s%3awhnemnxl9otmwgqem2g2qdrmm23oza4p.putn0wubp3cwrgoezzadbfoprtqnlentcfqfvvr%2fu7c; Io=wtqdo9octfs0qoboaaab "    },    "Method": "GET",    "Httpversion": "1.1",    "Originalurl": "/performance-now.js.map",    "Query": {}  },

3. Log records

The most common log records are: Access logs, error logs.

Recommended modules: Winston & Express-winston, LOG4JS use recommended: LOG4JS Log Management (https://www.cnblogs.com/yjfengwen/p/3827217.html)

Log app.use for the normal request (Expresswinston.logger ({  transports: [    New (Winston.transports.Console) ({      json:true,      colorize:true    }),    new Winston.transports.File ({      filename: ' Logs/success.log '    })  ])// Log app.use for the error request (Expresswinston.errorlogger ({  transports: [    new Winston.transports.Console ({      JSON: True,      colorize:true    }),    new Winston.transports.File ({      filename: ' Logs/error.log '    })  ]}))

4. Package configuration file

Module: package, for obtaining any configuration of the project Package.json profile

5. Page Message Management

Module: Connect-flash, because Flash is a special area in the session where information is stored, and this information is deleted after it is displayed to the user.

Therefore, it needs to be used in conjunction with the Express-session middleware.

How do I use it?

First, use the Cookieparser and session middleware to set the session:

Const FLASH = require (' connect-flash '= require (' Express '= require (' express-session '); var app = Express (); App.configure (function() {  app.use (Express.cookieparser ( ' keyboard cat ');   60000 }));  App.use (Flash ());});

After the flash middleware is used, all requests requests call the Flash () function to process the flash information.

function (req, res) {  //  by calling Req.flash (), set Flash prompt information  req.flash (' info ', ' Flash is back! ' )  res.redirect ('/');}); App.get (function(req, res) {  //  set Flash information  via key Res.render (' index ', {messages:req.flash (' info ')}) ;

Not to be continued ...

Nodejs with Learning (miscellaneous)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.