create an express project locally, access the remote mysql to add, delete, modify and query, and provide an API. There is no problem with local access to localhost:8080/getUsers.
install nodejs, and install express, on the CVM (Huawei Cloud I use) and then:
express product
create an express project, update the local bin, public, routes, views, app.js, package.json to the server product directory, install dependent npm install, and then:
pm2 start bin/www
808081
binmyServer.jsnode myServer.jsmyServer:
119...**:8081/list
node bin/wwwProduct.js
nodepm2
:
:
routes:
product.js:
function Product() {
this.name;
}
module.exports = Product;
products.js:
var express = require("express");
var router = express.Router();
var Product = require("./Product");
var URL = require("url");
/**
* product
*/
router.get("/getProducts", function(req, res, next) {
var product = new Product();
var params = URL.parse(req.url, true).query;
var productList = new Array();
var response = {status: params.id};
if(params.id == "1") {
product.name = "";
productList.push(product);
response.data = productList;
res.send(JSON.stringify(response));
} else {
var mysql = require("mysql");
var connection = mysql.createConnection({
host : "119.3.2.21",
user : "root",
password : "123Rfy123@",
port: "8635",
database: "my_project",
});
connection.connect();
var sql = "SELECT * FROM product";
connection.query(sql,function (err, result) {
if(err){
console.log("[SELECT ERROR] - ", err.message);
return;
}
// for(var i in result) {
//
// }
res.send(result);
});
}
});
module.exports = router;