[英]In an express application ,how can the javascript file in public folder access the database API written in routes folder under index.js
Aim of the project is,accessing the mysql database at localhost/phpmyadmin on clicking a button from the client side and getting the data from the database for filling up a table on client side. 该项目的目的是,在客户端上单击一个按钮并从数据库获取数据以填充客户端上的表,从而访问localhost / phpmyadmin上的mysql数据库。
This project is done using an express application. 该项目是使用快速应用程序完成的。 Code for database interface written under routes folder,
index.js
file: 数据库路由的代码写在路由文件夹下的
index.js
文件中:
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var connection = mysql.createConnection({
'host':'localhost',
'user':'root',
'password':'',
'database':'displayu'
});
connection.connect();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});
router.get('/getusers',function(req,res,next){
var x;
connection.query('SELECT * FROM sample', function(err,result,field){
if (err){
throw error;
}
else{
x = res.json(result);
console.log(x);
}
});
});
module.exports = router;
On localhost:3000
the client side code written in views folder under index.html
and the javascript logic under public folder results in a button and an empty table.I want to make use of the code written under server side routes folder onclick of this button to access the database and fill this data into the table. 在
localhost:3000
, index.html
下的views文件夹中编写的客户端代码和public文件夹下的javascript逻辑导致一个按钮和一个空表。我想利用在服务器端路由文件夹下编写的代码onclick此按钮访问数据库并将此数据填充到表中。 I don't know the exact steps to do this requirement. 我不知道执行此要求的确切步骤。 I have the client side code and the server side code but need help to link them both to get output.
我有客户端代码和服务器端代码,但需要帮助将它们链接在一起以获得输出。
As already suggested you can achieve that by doing an AJAX request from your client side js. 正如已经建议的那样,您可以通过从客户端js发出AJAX请求来实现。
You could do this with native JavaScript by using the XMLHttpRequest utilities. 您可以通过使用XMLHttpRequest实用程序使用本机JavaScript进行此操作。
For example: 例如:
function populateTable() {
// Handle your data here..
}
var GetUsersData = new XMLHttpRequest();
GetUsersData.addEventListener("load", reqListener);
GetUsersData.open("GET", "/getusers");
GetUsersData.send();
You could also use jQuery, depending on your project. 您还可以根据项目使用jQuery。 If you already use it, I'd recommend using the included jQuery.
如果您已经使用过它,我建议您使用随附的jQuery。 get() function, which is quite a bit simpler than using native JS.
get()函数,比使用本机JS简单得多。
If you are not using jQuery in your project at the moment, you should stick to doing it the JS native way, also depending on how many AJAX requests you want to make and also keeping in mind how many you have to manage/debug going forward. 如果您目前不在项目中使用jQuery,则应坚持使用JS本机方式,这还取决于要发出的AJAX请求数量,并记住以后必须管理/调试的数量。
Both of these ways are very interesting and I'd suggest reading up on at least one of them, as they are very useful and you will at some point have to use AJAX again. 这两种方式都非常有趣,我建议您至少阅读其中一种,因为它们非常有用,您有时会不得不再次使用AJAX。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.