简体   繁体   English

在快速应用程序中,公用文件夹中的javascript文件如何访问index.js下路由文件夹中编写的数据库API

[英]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:3000index.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.

相关问题 如何将value变量从javascript文件夹中的普通js文件转换为nodejs中的route / index.js文件并进行表达? - How do i take the value variable from a normal js file in the javascript folder to the routes/index.js file in nodejs and express? 使用 index.js 从“/文件夹”导入 javascript - javascript import from '/folder' with index.js 从公用文件夹中的脚本在index.js中运行功能 - Run function in index.js from script in public folder Express.js 4:如何访问app.locals。 <myvar> 在routes / index.js中? - Express.js 4: How to access app.locals.<myvar> in routes/index.js? node.js express 如何访问我的公共文件夹中的文件 - How to access files in my public folder in node.js express 无法将公共文件夹中的 index.html 与 src 文件夹中的 index.js 连接起来 - Unable to connect index.html in public folder with index.js in src folder 模块作为文件夹,但是该文件夹下的index.js仅包含一些require语句 - module as folder, but index.js under the folder only contains some require statements 将数组从 javascript 文件发送到 Express 中的 index.js - Send array from a javascript file to index.js in Express React,如何从文件夹 index.js 导入组件 - React, how to import components from folder index.js 如何在node / mean / express应用程序中拆分index.js文件? - How do I split up index.js file in a node/mean/express application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM