Basic use of Express

Posted by I Am Chris on Wed, 11 Dec 2019 01:35:07 +0100

Preface

  • List project Express is a brief and flexible node.js Web application framework, which provides a series of powerful features to help you create various Web applications and rich HTTP tools.

text

  • A simple example of express framework

        // Download express       
        cnpm install express --save 
        // Quote          
        import express from 'express';
        const app = express();
        // Use express to listen for the port number,
        app.listen(8080, function() {
            console.log('listen to 8080......'}
        )
        cnpm install nodemon --save // Replace node to start application
        // Write the following code in the scripts of package.json to start the application
        'dev': 'nodemon node build/app.js' /*Note: app.js is the name of your entry file*/
        

    The common middleware "body parser" of express is used to parse the request body in JSON, Raw, text and URL encoded formats. The object returned by 'bodyParser.urlencoded' is a key value pair. When extended is false, the value in the key value pair is in the form of 'String' or 'Array'. When true, it can be of any data type. The above two lines of code have covered most of the usage scenarios. If you need to explore the body parser in depth, please refer to Official documents.

        cnpm install body-parser --save;
        import bodyParser from 'body-parser';
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({ extended: false }));
    
  • Handle different request bodies

        app.use('*', (request, respose, next) => {
            res.header('Access-Control-Allow-Origin', '*'); // Specify allow other domain name access  
            res.header('Access-Control-Allow-Headers', 'Origin,X-Requested-With,content-Type,Accept,token,sysCode'); // Response header settings  
            res.header('Access-Control-Allow-Methods', 'POST,GET'); // Response type  
            res.header('X-Powered-By', '3.2.1'); // Hidden response
            res.header('Content-Type', 'application/plain;charset=utf-8'); // Mapping request information
            next();
        })
  • express routing
    Routing refers to how the endpoint of an application responds to a client's request. For more information about routing, see Basic routing;

       const router = express.Router();
       router.get('/api/addcart', (req, res) => {  
           res.send('hello world')
       })
  • Connect mysql

    // Download mysql 
    cnpm install mysql --save;
    // Introducing mysql
    import mysql from 'mysql';
    // The preparation work has been completed. The following code is to connect your mysql library.
    const connection = mysql.createConnection({
           host: 'localhost',
           user: 'root',
           password: 'root',
           port: '****', // Port set by your database
           database: "****" // The name of your database
       })
       connection.connect();
    
       // Then throw the name you wrote for later use
       export default connection;
       // Use the addition, deletion, modification and query of sql statements to complete all the effects you need!!!
       connection.query('select * from checkuser', function (req, result) {
           ck(result)
       })
    

Last

I am writing for the first time, and I would like to ask you for your understanding if there is any inadequacy in my writing. Finally, I enclose my github address , there are all the codes involved in this article, as well as some simple login, picture upload, sql statement addition, deletion, modification and query.

Topics: Javascript MySQL JSON Database SQL