当前位置:网站首页>Express learning notes (MOOC)

Express learning notes (MOOC)

2020-11-10 10:44:51 My sister is talking nonsense

( Review by yourself )
One 、 Create scaffolding
sudo npm i express-generator -g
mkdir express-cli
cd express-cli
express express-test
npm intsall
npm start

Two 、 The second step ( I don't know what title )

nodemon Monitoring code file changes , Restart at any time
cross-env Don't worry about platform settings or using environment variables
npm i nodemon cross-env --save-dev

bin: Compiled executable file
www To provide a http Service for .

publick 、views Front end with .

// package.json
  "dev": "cross-env NODE_ENV=dev nodemon ./bin/www"

3、 ... and 、 Introduce express Entry file

The role of each plug-in .

var cookieParser = require('cookie-parser'); analysis cookie
var logger = require('morgan'); For logging

app This time http Instance of request

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

Four 、 Demonstrates how to handle routing
New route file user.js,

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.post('/login', function (req, res, next) {
    const { username, password } = req.body;
    res.json({
        error: 0,
        data: {
            username,
            password
        }
    })
});
module.exports = router;

poatman Request mode :
image.png

5、 ... and 、 Middleware mechanism
put questions to :

  1. There's a lot of app.use...
  2. In the code next ginseng
//  middleware , The function in the middle .
//  Yes likeLogin, If you have any res return , The following function is not executed 
function likeLogin(req, res, next) {
    console.log(" Successful simulation login ");
    res.json({
        error: 0,
        data: [1, 2, 3]
    });
}
app.use('/api', likeLogin, (req, res, next) => {
    console.log("/api  app.post");
    next();
})

6、 ... and 、express summary :
How to handle logs ,cookie,
How to deal with routing ,
How to use middleware

7、 ... and 、 Build a blog system
image.png

版权声明
本文为[My sister is talking nonsense]所创,转载请带上原文链接,感谢