I write not complex, suitable for sprout new.

It is recommended to copy the code in your editor, this code is feasible (of course you have to have a database, can change the table name).

Mongodb or mysql database can be used, file header comments can be uncommented!

Copy the use of words, remember to change the ajax request url: “http://192.168.0.76:8089/test_post” (this is my address and path), you need to change yourself!!!!

/** * a Static Server * nodejs daemon */ const express = require('express'); // const mongodb = require('mongodb'); // const MongoClient = mongodb.MongoClient; Mysql = require(const mysql = require('mysql');

// const url = require('url'); // Body-parser is a popular Express middleware that parses the request body of POST requests. const bodyParser = require('body-parser'); // Used to get the POST request parametersletUrlencodedParser = bodyParser. Urlencoded ({// Extended for post request parameters:false

})

letapp = express(); // Resolve cross-domain app.all(The '*'.function(req, res, next) {

res.header("Access-Control-Allow-Origin"."*");

res.header("Access-Control-Allow-Headers"."Content-Type,Content-Length, Authorization, Accept,X-Requested-With");

res.header("Access-Control-Allow-Methods"."PUT,POST,GET,DELETE,OPTIONS");

if(req.method == "OPTIONS") {

res.send(200);

} else{ next(); }}); // Set the static file directory app.use(express.static('/')); /* A test */ / a middleware is required to get the POST request parameters, on top of which the app is loaded. Post ('/test_post',urlencodedParser, (req, res) => { console.log(req.body,req.originalUrl) res.send(req.body); }) // Get the request parameter app.get('/test_get', (req, res) => {

var num = 200;

//res.send('Connection successful, this string is the data returned by node.js server',num) // comma separation is used to set the status code returned with the data such as 200 (success) 500 (server error) and so on. //res.status(200); // Res.status (200); //res.send('Connection successful, this string is the data returned by node.js server'+ num) / / separated by a plus sign is to return the number as data to the console. The log (the req. Query, the req. OriginalUrl) res. Send (the req. Query); }) /* A test */ // register app.get('/zhuce123', (req, res) => {

var pool = mysql.createPool({

host: 'localhost',

user: 'root',

password: ' ',

port: 3307,

database: 'vuedata',

multipleStatements: true

    });

    console.log(req.query.yonghum);

    var sql1 =`insert into yonghubiao (usname, password) values ('${req.query.yonghum}'.'${req.query.xinmima}')`

pool.query(sql1, function(error, rows) { res.send(req.query.yonghum); }); }) // Log in to app.get('/denglu123', (req, res) => {

var pool = mysql.createPool({

host: 'localhost',

user: 'root',

password: ' ',

port: 3307,

database: 'vuedata',

multipleStatements: true

    });

    // console.log(req.query.yonghum); 

    var sql1 =`select * from yonghubiao where usname='${req.query.yonghum}' and password = '${req.query.xinmima}' limit1 `; pool.query(sql1,function(error, rows) {

        console.log(rows.length);

        if(rows.length ! = 0){ res.send("1");

        }else{

            res.send("0"); }}); }); // Change the password app.get('/xiugai123', (req, res) => {

var pool = mysql.createPool({

host: 'localhost',

user: 'root',

password: ' ',

port: 3307,

database: 'vuedata',

multipleStatements: true

});

// var sql = ;

pool.query(`update yonghubiao set password='${req.query.xinmima}' where usname='${req.query.yonghum}'`, function(error, rows) {

res.send("1"); console.log(req.query); }); }); // app.post()'/login', urlencodedParser, (req, res) => {

// let {

// name,

// mima

// } = req.body;

// MongoClient.connect("mongodb://localhost:27017/".function(err, database) {

// if(err) throw err;

// let db = database.db('xcy');

// let yhb = db.collection('yhb'); FindOne ({// username: name, // password: mima //}, (err, result) => {//ifSend ({// code: 1, // data: result, // MSG: token // res.send({// code: 1, // data: result, // MSG:'ok'/ / / /}})else {

// res.send({

// code: 0,

// data: [],

// msg: 'fail'//}) //} //}); // database.close(); / /}); / /}); // // User list // app.get('/hqyh', (req, res) => {

// var pool = mysql.createPool({

// host: 'localhost',

// user: 'root',

// password: ' ',

// port: 3307,

// database: 'xcy',

// multipleStatements: true

// });

// pool.query('select * from yonghubiao'.function(error, rows) { // res.send(rows); / /}); / /}); // // Change password // app.get('/xiugai', (req, res) => {

// var pool = mysql.createPool({

// host: 'localhost',

// user: 'root',

// password: ' ',

// port: 3307,

// database: 'xcy',

// multipleStatements: true

// });

// // var sql = ;

// pool.query(`update yonghubiao set password='${req.query.xinmima}' where usname='${req.query.yonghum}'`, function(error, rows) {

// res.send(""); // console.log(req.query); / /}); / /}); // // delete // app.get('/shanchu', (req, res) => {

// var pool = mysql.createPool({

// host: 'localhost',

// user: 'root',

// password: ' ',

// port: 3307,

// database: 'xcy',

// multipleStatements: true

// });

// // var sql =;

// pool.query(`DELETE FROM yonghubiao where usname= '${req.query.yonghum}'`, function(error, rows) {

// res.send("Deleted successfully"); // console.log(req.query.yonghum); / /}); / /}); // // Add // app.get('/tianjia', (req, res) => {

// var pool = mysql.createPool({

// host: 'localhost',

// user: 'root',

// password: ' ',

// port: 3307,

// database: 'xcy',

// multipleStatements: true

// });

// console.log(req.query);

// var sql = `insert into yonghubiao (usname, password) values ('${req.query.yonghum}'.'${req.query.xinmima}') `; // pool.query(sql,function(error, rows) {

// res.send("Added successfully"); / /}); / /}); // // list of products // app.get('/splb', (req, res) => {

// var pool = mysql.createPool({

// host: 'localhost',

// user: 'root',

// password: ' ',

// port: 3307,

// database: 'xcy',

// multipleStatements: true

// });

// pool.query('select * from goods'.function(error, rows) { // res.send(rows); / /}); / /}); // // Modify product list // app.get('/xiugaigood', (req, res) => {

// var pool = mysql.createPool({

// host: 'localhost',

// user: 'root',

// password: ' ',

// port: 3307,

// database: 'xcy',

// multipleStatements: true

// });

// var sql = `

// update goods set goodname='${req.query.goodming}' where id='${req.query.goodid}';

// update goods set jiage='${req.query.goodjiage}' where id='${req.query.goodid}';

// update goods set goodxiangqing='${req.query.goodzhonglei}' where id='${req.query.goodid}';

// update goods set tupianlujing='${req.query.goodtupianlujing}' where id='${req.query.goodid}';

// update goods set jieshao='${req.query.goodjieshao}' where id='${req.query.goodid}';

// update goods set shouchu='${req.query.goodshouchu}' where id='${req.query.goodid}';

// update goods set rongliang='${req.query.goodrongliang}' where id='${req.query.goodid}';

// update goods set id='${req.query.goodnewid}' where id='${req.query.goodid}'; / / `; // pool.query(sql,function(error, rows) {

// res.send(""); // console.log(req.query); / /}); / /}); // // delete good // app.get('/shanchugood', (req, res) => {

// var pool = mysql.createPool({

// host: 'localhost',

// user: 'root',

// password: ' ',

// port: 3307,

// database: 'xcy',

// multipleStatements: true

// });

// // var sql =;

// pool.query(`DELETE FROM goods where id= '${req.query.goodid}'`, function(error, rows) {

// res.send("Deleted successfully"); // console.log(req.query.goodid); / /}); / /}); // // Add goods // app.get('/tianjiagood', (req, res) => {

// var pool = mysql.createPool({

// host: 'localhost',

// user: 'root',

// password: ' ',

// port: 3307,

// database: 'xcy',

// multipleStatements: true

// });

// console.log(req.query);

// var sql = `insert into goods (goodname, id,jiage,goodxiangqing,tupianlujing,jieshao,shouchu,rongliang) values ('${req.query.goodming}'.'${req.query.goodnewid}'.'${req.query.goodjiage}'.'${req.query.goodzhonglei}'.'${req.query.goodtupianlujing}'.'${req.query.goodjieshao}'.'${req.query.goodshouchu}'.'${req.query.goodrongliang}') `; // pool.query(sql,function(error, rows) {

// res.send("Added successfully"); / /}); / /}); App.listen (8089, () => {console.log()"Listening successful, server running ing, port :8089")})Copy the code

Here is the matching request, the same with axios for the Vue project, just for parameter formatting

<! DOCTYPE html> <html> <head> <meta charset="utf-8">

<title></title>

<style type="text/css">

html,

body {

width: 100%;

height: 100%;

}

#btn {

width: 100px;

height: 100px;

margin: 0 auto;

display: block;

margin-top: 35%;

transform: translateY(-50%);

}

#btn2 {

width: 100px;

height: 100px;

margin: 0 auto;

display: block;

margin-top: 15%;

transform: translateY(-50%);

}

#txt {

width: 360px;

margin: 0 auto;

display: block;

padding: 10px;

background-color: bisque;

}

</style>

</head>

<body>

<button type="button" id="btn"> Send BTN </button> <buttontype="button" id="btn2"> send btn2</button> <div id="txt">xxc </div>

<script src="common.js" type="text/javascript" charset="utf-8"></script>

<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>

<script type="text/javascript">

var btn = document.getElementById("btn");

var btn2 = document.getElementById("btn2");

var txt = document.getElementById("txt");

btn.onclick = function(e) { var ajax = new XMLHttpRequest(); // If get is passed, use the string template to write in the URL ajax.open('POST'.'http://192.168.0.76:8089/test_post');

ajax.setRequestHeader("Content-Type"."application/x-www-form-urlencoded"); // Native Ajax sends data in a compact format with no Spaces (json.parse will cause problems)."name=yyy&abc=123"); // Carry data to the server ajax.onreadyStatechange =function() {

if (ajax.readyState == 4 && ajax.status == 200) {

var res = JSON.parse(ajax.responseText);

console.log(res);

txt.innerHTML = (res.name +""+res.abc); }} // jquery version $("#btn2").click(function() {

//get

$.ajax({

type: "get",

url: "http://192.168.0.76:8089/test_get",

data: {

name: 'Like it if you think it's okay and follow it.'

},

success: function(res) {

console.log(res)

$("#txt").html(res.name); }}); //post // $.ajax({ //type: "post",

// url: "http://192.168.0.76:8089/test_post",

// data: {

// name: 'qwe'

// },

// success: function(res) {

// $("#txt").html(res.name);

// }

// });

});

</script>

</body>

</html>
Copy the code