Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql NodeJS是否可以在一个routes页面中运行多个sql查询?_Mysql_Node.js_Database_Express - Fatal编程技术网

Mysql NodeJS是否可以在一个routes页面中运行多个sql查询?

Mysql NodeJS是否可以在一个routes页面中运行多个sql查询?,mysql,node.js,database,express,Mysql,Node.js,Database,Express,我正在尝试使用NodeJS从mysql数据库中提取数据。我需要运行多个路由,但是否可以在一个routes.js页面中运行这些路由?我将在下面附上我的代码,但当我将路由输入浏览器ie localhost:3000/a时,它不起作用,我希望能够有多个,如localhost:3000/b和c与d 我尝试了不同的路线和路径,但无法使它们工作。任何帮助都是感激的 app.js var express = require('express'); var path = require('path'); var

我正在尝试使用NodeJS从mysql数据库中提取数据。我需要运行多个路由,但是否可以在一个routes.js页面中运行这些路由?我将在下面附上我的代码,但当我将路由输入浏览器ie localhost:3000/a时,它不起作用,我希望能够有多个,如localhost:3000/b和c与d

我尝试了不同的路线和路径,但无法使它们工作。任何帮助都是感激的

app.js

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mysql= require('mysql');
var http = require('http');


var index = require('./routes/index');
var users = require('./routes/users');
var candidates = require('./routes/users')

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));


//Database connection
app.use(function(req, res, next){
    global.connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        database : 'irlelection2020'
    });
    connection.connect();
    next();
});

app.use('/api/parties', users);
app.use('/api/candidates', candidates);


module.exports = app;
var server = http.createServer(app);
server.listen(3000);
console.log("Server running on port 3000");
users.js(routes.js)

index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {

        res.render('index', { title: 'Express' });
});

router.get('/candidates', function(req, res, next) {

    res.render('index', { title: 'Express' });
});


module.exports = router;

这里发生了几件事。对于以下代码:

var users = require('./routes/users');
var candidates = require('./routes/users');
命名这些不同的变量并不意味着在通过以下方式声明路由时会发生任何奇迹:

app.use('/api/parties', users);
app.use('/api/candidates', candidates);
两个端点都指向同一个文件,并将匹配它们遇到的第一条路由。在您的users.js文件中,您以完全相同的方式定义了两条路由:

router.get('/', function(req, res, next) {
// ...
router.get('/', function(req, res, next) {
因此,无论何时您访问
/api/parties
/api/candidates
,他们都会从该文件中点击您的第一条路线。您基本上有两个选择:

  • 制作两个单独的路由文件
  • 更改您声明路线的方式
  • 对于方法1,您只需将候选代码从users.js移动到一个新文件,比如candidates.js,然后更改此定义:

    var candidates = require('./routes/candidates');
    
    对于方法2,您可以将它们保存在同一个文件中,但必须将app.js中的基本路由更改为:

    app.use('/api', users);
    
    然后在users.js中,您将按如下方式声明路由:

    router.get('/parties', function(req, res) {
    // ...
    router.get('/candidates', function(req, res) {
    
    此外,您还希望将实际路由函数更改为
    函数(req,res)
    ,而不是
    函数(req,res,next)
    。您还可以
    res.json
    缩短json响应的时间:

    res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
    


    这里发生了几件事。对于以下代码:

    var users = require('./routes/users');
    var candidates = require('./routes/users');
    
    命名这些不同的变量并不意味着在通过以下方式声明路由时会发生任何奇迹:

    app.use('/api/parties', users);
    app.use('/api/candidates', candidates);
    
    两个端点都指向同一个文件,并将匹配它们遇到的第一条路由。在您的users.js文件中,您以完全相同的方式定义了两条路由:

    router.get('/', function(req, res, next) {
    // ...
    router.get('/', function(req, res, next) {
    
    因此,无论何时您访问
    /api/parties
    /api/candidates
    ,他们都会从该文件中点击您的第一条路线。您基本上有两个选择:

  • 制作两个单独的路由文件
  • 更改您声明路线的方式
  • 对于方法1,您只需将候选代码从users.js移动到一个新文件,比如candidates.js,然后更改此定义:

    var candidates = require('./routes/candidates');
    
    对于方法2,您可以将它们保存在同一个文件中,但必须将app.js中的基本路由更改为:

    app.use('/api', users);
    
    然后在users.js中,您将按如下方式声明路由:

    router.get('/parties', function(req, res) {
    // ...
    router.get('/candidates', function(req, res) {
    
    此外,您还希望将实际路由函数更改为
    函数(req,res)
    ,而不是
    函数(req,res,next)
    。您还可以
    res.json
    缩短json响应的时间:

    res.send(JSON.stringify({"status": 200, "error": null, "response": results}));
    


    所以在所有这些之后,我将能够搜索例如“localhost:3000/api/candidates和同一方”“它会显示不同的数据吗?我不确定你在问什么。如果要使用同一路线显示来自不同参与方的候选人,则需要执行类似于
    /api/:partyId/candidates
    的操作,然后根据该id修改SQL查询。您可以在此处查看快速路线文档:完美,让这一切顺利进行,它的工作非常有助于非常感谢。所以在所有这些之后,我将能够搜索例如“localhost:3000/api/candidates and the same”party“它会显示不同的数据吗?我不确定你在问什么。如果要使用同一路线显示来自不同参与方的候选人,则需要执行类似于
    /api/:partyId/candidates
    的操作,然后根据该id修改SQL查询。您可以在此处查看快速路线文档:完美,非常感谢。您的目标是使用一个routes文档(在您的示例中为
    users.js
    )-并且当请求的路径为
    /api/parties
    时,您的
    选择*from parties
    查询将运行,但是,当请求的路径为
    /api/candidates
    时,将运行
    从候选人中选择*
    查询?您的目标是使用一个routes文档(在您的示例中为
    users.js
    )-当请求的路径为
    /api/parties
    时,将运行
    从参与者中选择*
    查询,但是当请求的路径为
    /api/candidates
    时,您的
    SELECT*from candidates
    查询将运行吗?