Home >Web Front-end >JS Tutorial >How does the nodejs interface transmit data?

How does the nodejs interface transmit data?

青灯夜游
青灯夜游forward
2020-09-12 11:04:063755browse

How does the nodejs interface transmit data?

Video tutorial recommendation: node js tutorial

Recent projects require interface transmission, so I looked for which one to choose Language, select node, and it is https mode!
It took me a long time to solve the cross-domain problem! Without further ado, let’s go straight to the code

let mysql = require('mysql');
let express = require('express');
let app = express();
let https = require("https");
let fs = require("fs");
// Configuare https
const httpsOption = {
    key : fs.readFileSync("./https/3_jdong.xuexuebang.cn.key"),//https证书key
    cert: fs.readFileSync("./https/2_jdong.xuexuebang.cn.crt")//https证书crt
}
//链接数据库
let connection = mysql.createConnection({
    host     : '127.0.0.1',
    port     : '3306',
    database : 'sz',

    user     : 'soubei',
    password : 'soubei',
})

connection.connect();

//解决跨域问题
app.all("*",function (req, res, next) { //允许所有请求方式
    res.header("Access-Control-Allow-Origin","*");//所有
    res.header("Access-Control-Allow-Headers","content-type")//post
    next()
})

app.get('/userlist',function(req,res){
     shop_name = req.query.shop_name;


    connection.query('SELECT * from `order` where orderStatus="暂停" ',function(error,results,fileds){
        if(error) throw error;
        res.header("Access-Control-Allow-Origin", "*");
        res.header('Access-Control-Allow-Headers', 'Content-type');
        res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS,PATCH");
        res.header('Access-Control-Max-Age',1728000);//预请求缓存20天

        res.writeHead(200,{"Content-Type":"text/json;chartset=utf-8"})
        data = []
         // console.log('SELECT skuId from `cmf_order_sku` where shop_name="'+shop_name+'" group by skuId')
           connection.query('SELECT skuId from `cmf_order_sku` where shop_name="'+shop_name+'" group by skuId',function(error,result,fileds){
            if (result !=''){
               res.end(JSON.stringify(result))

            }
             if (error == null){

                res.end(JSON.stringify('请求失败'))
            }

          })

    })
})

https.createServer(httpsOption, app).listen(8080,function(){
    // let host = server.address().address;
    // let port = server.address().port;
    console.log("应用实例,运行在http://%s:%s")
});

The renderings are as follows

How does the nodejs interface transmit data?

For more programming-related knowledge, please visit:Programming Teaching ! !

The above is the detailed content of How does the nodejs interface transmit data?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete