使用express作爲前端和後臺的中間層Demo

搭建測試服務器

使用之前Spring boot項目的測試demo

創建nodejs創建express項目

var express = require("express");
var http = require("http");
var app = express();


app.get('/',function(req,res){
    console.log("hello express");
    res.send("hello express");
});


app.get('/add',function(req,res){
    console.log("hello express add");
    var options = {
        host:'127.0.0.1',
        port:8080,
        path:'/add',
        method:'post'
    }
    var requestServer = http.request(options,function(reqSer,resSer){
        console.log('STATUS: ' + reqSer.statusCode); 
        console.log('HEADERS: ' + JSON.stringify(reqSer.headers)); 
        reqSer.setEncoding('utf8'); 
        reqSer.on('data', function (chunk) { 
            console.log('BODY: ' + chunk); 
            res.writeHead(200, {"Content-Type": "text/plain"});  
            res.write(chunk);
            res.end();  
        }); 
    });
    requestServer.on('error', function (e) { 
        console.log('problem with request: ' + e.message); 
    });
    requestServer.end();
});

var server = app.listen(8888, function () {
  var host = server.address().address
  var port = server.address().port
  console.log("應用實例,訪問地址爲 http://%s:%s", host, port)
})

運行效果

1.瀏覽器
這裏寫圖片描述

2.nodejs後臺
這裏寫圖片描述

3.服務器後臺
這裏寫圖片描述

總結

服務器後臺是調用python的一個測試demo,不清除的朋友只需將其當成一個controller對應的返回數據即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章