Overview
Express is a simple and flexible node. JS WEB Application framework that provides a range of powerful features to help you create a variety of Web applications, and rich HTTP tools.
Express can be used to quickly build a full-featured website.
Express Framework Core Features:
You can set up middleware to respond to HTTP requests.
Defines the routing table used to perform different HTTP request actions.
You can render HTML pages dynamically by passing parameters to the template.
Installation:
NPM Install Express
NPM Install Express--save
Associated Middleware Installation:
NPM Install Body-parser--save
NPM Install Cookie-parser--save
NPM Install Multer--save
The above command installs the Express framework in the Node_modules directory of the current directory, and the Express directory is automatically created in the Node_modules directory.
Body-parser-node.js Middleware for processing (ContentType) JSON, Raw, Text, and URL-encoded data.
Cookie-parser-This is a tool for parsing cookies. The req.cookies can be used to fetch the transmitted cookies and turn them into objects.
Multer-node.js middleware for processing form data that enctype= "Multipart/form-data" (Sets the MIME encoding of the form).
Express Middleware Body-parser
The Express project usually uses Body-parser to parse the post parameters, the most commonly used is the JSON and the urlencoded parser, which can parse the post parameters in JSON format and the urlencoeded post parameters, respectively. Can get a JSON req.body.
Reference:
Using Body-parser in Express projects
Body-parser GitHub Project
Express API:
//Access Express static Resources linkvarExpress = require (' Express ');varApp =Express (); App.use (Express.static (' Public '));//Project Catalogue/npm_modules/public/index.html//StartvarServer = App.listen (8081,function () {})//Visit http://serverIp:port/index.html//use as interface server linkvarExpress = require (' Express ');varBodyparser = require (' Body-parser '));//application/x-www-form-urlencoded Encoding parsingvarParseform = bodyparser.urlencoded ({extended:false});//Application/json Encoding parsingvarParsejson =Bodyparser.json ();varHttpreq =Express ();//http://localhost:8081/index/IndexPage.htmlHttpreq.use (express.static (' public ')));functionTestexpressapi () {/** * Express GET request * http://localhost:8081/obtain/clientOnlineState?name=aa01&password=010203*/Httpreq.get ('/obtain/clientonlinestate ',function(req, resp) {console.log (req.query); Resp.end (Json.stringify (req.query)); }); /** * Express POST Request * Http://localhost:8081/update/remoteClientInfo * {"name": "Aa01", "Password": "0102 "}*/Httpreq.post ('/update/remoteclientinfo ', Parsejson,function(req, resp) {console.log (req.body); Resp.end (Json.stringify (req.body)); }); /** * Server monitoring 8081 Port * @type {*}*/ varServer = Httpreq.listen (8081,function () { //var host = Server.address (). Address; varPort =server.address (). Port; Console.log ("Http://localhost:%s", Port)});
Express development issues:
Q1. Getting the remote connection IP address
var ip = req.headers[' x-forwarded-for ' | | req.connection.remoteAddress;
Reference:
Express.js:how to get remote client address
Nodejs Express can not get the user's external network IP address solution
Reference:
Express 4.x API Chinese manual API
node. JS Express Framework Runoob Basic Tutorial
Introduction to Nodejs Express usage
Nodejs Study notes (v)---Express installation primer and template engine Ejs
Body-parser-github Official API
Expressjs-install
Supplemental knowledge
P1. Several common ways to submit data in post
The server is typically based on the Content-type field in the request header (headers) to learn how the message body in the request is encoded and then parses the subject. So when it comes to the POST submission data scheme, it contains two parts: the Content-type and the message body encoding method.
application/x-www-form-urlencoded
The most common way of POST submission data. The native form form of the browser, and if you do not set the Enctype property, the data will eventually be submitted in application/x-www-form-urlencoded manner.
Content-type is designated as application/x-www-form-urlencoded; second, the submitted data is encoded in key1=val1&key2=val2 manner, and both key and Val are URL-transcoded. Most of the service-side languages are very supportive of this approach. For example, in PHP, $_post[' title ' can get to the value of title, $_post[' Sub '] can get a sub array.
Application/json
Application/json this content-type as the response header tells the server that the message body is a serialized JSON string. The JSON format supports much more complex structured data than key-value pairs.
Multipart/form-data
This is another common way of POST data submission. When we use a form to upload a file, the enctyped of the form must be equal to this value.
Text/xml
It is a remote invocation specification that uses HTTP as the transport protocol and XML as the encoding method.
Reference:
Application/json Four common POST-submission data types
Analysis on the difference between application/x-www-form-urlencoded and Multipart/form-data
Nodejs Web Services (Express)