----- Note: This series of recorded knowledge blog mainly from dark horse programmers Nodejs video tutorials, bloggers simply manually code reuse existing knowledge and records
Express
Http native performance in some areas is not sufficient to cope with our development needs, so we need to use the framework to accelerate our development efficiency. The purpose of the framework is to improve efficiency, so that our code more uniform height
1. start
1.1 installation:
npm install --save express
var express = require('express') var app = express() app.get('/',function(req,res){ //推荐使用express的方法 res.send("hello world") }) app.listen(3000,function(){ console.log("express app is running ...") })
1.3 Basic Routing
router
-
Request method
-
Request path
-
Request handler
// When GET method request to / when performing a process corresponding to the function app.get ( '/', function (REQ, RES) { res.send ( 'Hello World!' ) })
post:
// When the POST method request to / when performing a process corresponding to the function app.post ( '/', function (REQ, RES) { res.send ( 'the Get Request POST A' ) })
// /public资源 app.use(express.static('public')) // /files资源 app.use(express.static('files')) // /public/xxx app.use('/public',express.static('public')) // /static/xxx app.use('/static',express.static('public')) app.use('/static',express.static(path.join(__dirname,'public')))
2. Express configured to use in the art-template
template engine
npm install --save art-template
npm install --save express-art-template
Configuration:
// Default Art app.engine ( 'Art', the require ( 'Template-Express-Art' )) // modified HTML app.engine ( 'HTML', the require ( 'Template-Express-Art'))
use:
// use the html modify the configuration of app.get ( '/', function (REQ, RES) { // Express default will go to projects in the views directory to find index.html res.render ( 'index.html' , { title: 'Hello World' }) })
// The first parameter views do not mistake app.set ( 'views', the directory path)
3. Obtain Form GET request parameters in Express,
Express built-in API, you can directly through req.query
req.query
Note: In the browser address input, the default is the get request
4. Gets the data in the POST request body Express,
No built-in API to obtain a form POST request body in Express, where we need to use a third-party package:body-parser
installation:
npm install --save body-parser
Configuration:
var Express = the require ( 'Express' ) // of 0. The primer packet var bodyParser the require = ( 'body-Parser' ) var App = Express () // 1. Configure-Parser body // Just add this configuration, then req request will be more out of a property on an object: body // That can directly obtain the data through a form POST request body the req.body // the parse file application / X-WWW-form-urlencoded app.use (bodyParser.urlencoded ({ Extended: to false })) // the parse file application / JSON app.use (bodyParser.json ())
use:
app.use ( function (REQ, RES) { res.setHeader ( 'the Content-the Type', 'text / Plain' ) res.write ( 'you. Posted: \ n-' ) // may be acquired by a form POST req.body data request body res.end (the JSON.stringify (req.body, null , 2 )) })
5. Use the tool to automatically restart the service nodemon
Here we can use a third-party tool named: nodemon
to help us solve problems frequently modify the code to restart the server
nodemon
Is based on a third-party command-line tool Node.js development, we need to use a stand-alone installation:
# Execute this command in any directory can have # That is all that is required - , Ltd. Free Join to install the package can be performed in any directory npm install --global nodemon
After installation, use:
# Before using the Node
the Node app.js
# now use nodemon
nodemon app.js
6.Express - crud (routing)
6.1 modularization
How module division:
-
Responsibility to a single module
-
View
-
angular
-
React
-
It is also very conducive to learning the three front-end framework
6.2 start
-
initialization
-
Template processing
6.3 Routing Design
6.4 Extraction routing module
router.js:
/ * Router.js routing module functions: handles the routing depending on the route setting request method + requests a specific request handler * / // modules to a single role, do not write without // object is to enhance partitioning module project code maintainable resistance, improve development efficiency var Express = the require ( 'Express' ) // 1. Create a routing container var router = express.Router () // 2. the router routes routes are mounted to the vessel router.get (' / Students. ', function (REQ, RES) { }) router.get ( ' / Students. / new new ', function (REQ, RES) { res.render ( ' new.html ' ) }) router.post ( ' / Students. / new new ', function (REQ, RES) { }) router.get('/students/edit',function(req,res){ }) router.post('/students/edit',function(req,res){ }) router.get('/students/delete',function(req,res){ }) //3.把 router 导出 module.exports = router
app.js:
var Router = the require ( "./ Router" ) // mount routing app.use (router)
/ * Student.js data file module operating duties: operational data files, data processing only, do not care about the business * / / * get a list of all students return array * / exports.find = function () { } / * Add save students * / exports.save = function () { } / * update student * / exports.update = function () { } / * delete student * / Exports. the delete = function () { }
-
Process Template
-
Configuration Open static resources
-
Configuration template engine
-
Simple routing: / students render a still page out
-
Routing Design
-
Extraction routing module
-
As the next new business operations need to deal with file data, so we need to package student.js
-
First written student.js file structure
-
Query API find a list of all students
-
findById
-
save
-
updateById
-
deleteById
-
-
Achieve specific functions
-
Receipt of the request by routing
-
Receiving a data request (get, post)
-
req.query
-
req.body
-
-
Manipulating data using the data processing API
-
The client sends a response to the result of the operation
-
-
Order of business functions
-
List
-
Add to
-
edit
-
delete
-
-
ES6 two important API: find, findIndex